74

I tried ps with different kinds of switches e.g. -A, aux, ef, and so forth but I cannot seem to find the right combination of switches that will tell me the Process ID (PID), Parent Process ID (PPID), Process Group ID (PGID), and the Session ID (SID) of a process in the same output.

Josh Correia
  • 236
  • 2
  • 8
JohnMerlino
  • 5,941
  • 11
  • 34
  • 38

3 Answers3

101

Here you go:

$ ps  xao pid,ppid,pgid,sid | head
  PID  PPID  PGID   SID
    1     0     1     1
    2     0     0     0
    3     2     0     0
    6     2     0     0
    7     2     0     0
   21     2     0     0
   22     2     0     0
   23     2     0     0
   24     2     0     0

If you want to see the process' name as well, use this:

$ ps  xao pid,ppid,pgid,sid,comm | head
  PID  PPID  PGID   SID COMMAND
    1     0     1     1 init
    2     0     0     0 kthreadd
    3     2     0     0 ksoftirqd/0
    6     2     0     0 migration/0
    7     2     0     0 watchdog/0
   21     2     0     0 cpuset
   22     2     0     0 khelper
   23     2     0     0 kdevtmpfs
   24     2     0     0 netns
terdon
  • 234,489
  • 66
  • 447
  • 667
  • 3
    What does "comm" and "head" do? – JohnMerlino Jul 12 '13 at 14:49
  • 5
    @JohnMerlino "comm" prints the command name and `head` is a different command, nothing to do with `ps`, it just prints the first N (10 by default) lines, I used it here to limit the size of my answer. Everything you need to know about ps is explained in `man ps`. – terdon Jul 12 '13 at 15:09
  • 1
    `comm` does not print the full command. E.g, if you run `python foo.py`, comm will only show you the `python` part, but not `python foo.py` – 32r34wgf3e Jan 29 '18 at 22:19
  • 3
    Yes, that's why I said it prints the command's _name_. The command is `python`, the `foo.py` is the command's argument. Try `cmd` if you want the arguments too. – terdon Jan 29 '18 at 22:26
26

Try

ps -efj | less

Specifically, if you want to find out PID/PGID/PPID/SID for a certain ProcessName or PID, Try:

ps -efj | grep ProcessName

ps -efj | grep PID

OR for better-formatted output, try:

ps -ejf | egrep 'STIME|ProcessName'
    
ps -ejf | egrep 'STIME|pid'

Examples:

ps -ejf | egrep 'STIME|http'

ps -ejf | egrep 'STIME|1234'

SAMPLE:

[ram@thinkred1cartoon ~]$ ps -ejf | egrep 'STIME|http'

UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
root      1450     1  1450  1450  0 08:45 ?        00:00:04 /usr/sbin/httpd -DFOREGROUND
ram       3717     1  2589  2589  0 08:47 ?        00:00:00 /usr/libexec/gvfsd-http --spawner :1.3 /org/gtk/gvfs/exec_spaw/1
apache   11518  1450  1450  1450  0 09:40 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11519  1450  1450  1450  0 09:40 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11520  1450  1450  1450  0 09:40 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11521  1450  1450  1450  0 09:40 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11522  1450  1450  1450  0 09:40 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

Assuming 1234 is PID and you want to know its associated PPID, PGID, SID & CMD


Tested on CentOS/RedHat 6.x 7.x and 8.x

Raman Kathpalia
  • 547
  • 1
  • 7
  • 14
0

Try with below command and worked fine

ps -eo pid,ppid,pgid,sgid| head
  PID  PPID  PGID  SGID
    1     0     1     0
    2     0     0     0
    4     2     0     0
    6     2     0     0
    7     2     0     0
    8     2     0     0
    9     2     0     0
   10     2     0     0
   11     2     0     0
Seamus
  • 2,522
  • 1
  • 16
  • 31
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14
  • Looking at [the existing accepted answer](https://unix.stackexchange.com/a/82727/117549) of `ps xao pid,ppid,pgid,sid`, how is your answer better? It seems you replaced `sid` (session id) with `sgid`, which is the saved group ID. The Question asks for: *Process ID (PID), Parent Process ID (PPID), Process Group ID (PGID), and the Session ID (SID) of a process*, so yours is missing the Session ID. You've also been told before that your prompt is not an important part of the Answer. – Jeff Schaller May 13 '21 at 11:57
  • Yes I will take care of this next time – Praveen Kumar BS May 13 '21 at 15:38