How do I get the parent process ID of a given child process in Linux.

Use

ps -o ppid=

 

  • e.g. ps -o ppid= 2076 returns 2054, 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 | grepps -eo ppid,comm | grep '[f]oo'.
  • Fuller explanation: ps -f 2072 returns
    UID        PID  PPID  C STIME TTY      STAT   TIME CMD
    izx       2076  2054  0 07:16 ?        S      0:00 /usr/lib/pulseaudio/pulse/gconf-helper
    
  • The pstree relation is: pstree -s -p 2076:
    init(1)───pulseaudio(2054)───gconf-helper(2076)

OR

Step 2:-

echo $PPID

if you need the command from this parent pid:

cat /proc/$PPID/comm

if you need the full command line (with all options):

cat /proc/$PPID/cmdline

Explanation

  • $PPID is defined by the shell, it’s the PID of the parent process
  • in /proc/, you have some dirs with the PID of each processes. Then, if you cat /proc/$PPID/comm, you echo the command name of the PID

That’s it for now !