Ubuntu – How to get the parent process ID of a given child process

bashcommand lineprocess

How to get parent PID from a given child's PID?

I know I can manually check it under /proc, I wonder if there is a smart/better way to achieve this in Ubuntu. Note the parent may or may not be killed.

Thanks

Best Answer

How to get a parent PID (PPID) from a child's process ID (PID) using the command-line

Use ps -o ppid=

  • e.g. ps -o ppid= 2072 returns 2061, which you can easily use in a script etc. ps -o ppid= -C foo gives the PPID of process with command foo. You can also use the old fashioned ps | grep: ps -eo ppid,comm | grep '[f]oo'.
  • Fuller explanation: ps -f 2072 returns
    UID        PID  PPID  C STIME TTY      STAT   TIME CMD
    izx       2072  2061  0 07:16 ?        S      0:00 /usr/lib/pulseaudio/pulse/gconf-helper
    
  • The pstree relation is: pstree -s -p 2072:
    init(1)───pulseaudio(2061)───gconf-helper(2072)