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 | grep: ps -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 !

Akash Angle

I am a Full time Linux user who has quit using Windows for unknown reasons, making my life truly open source.

Recent Posts

Get the most juice from your ISP/router — setting MTU size & other handy tweaks

This is not an ad-vocation by any means for TP-link branding, however a real life…

8 months ago

How to make any Android phone up-to 3x faster – Developer options unleashed

First of all we need to hit the kernel version on any Android device. You…

8 months ago

systemd-resolve command not found in Ubuntu Desktop

Use resolvectl status instead. It's like something deprecates and suddenly things get broken! In systemd…

1 year ago

How to exclude multiple directories with rsync?

Geeky question: This is what people and friends have tried: rsync -arv --exclude "/home/john/.ccache:/home/ben/build" /home/john…

1 year ago

How to resolve apt-get -f not working

You might encounter this error which appears to be very common on Debian based Distro's…

1 year ago

How to install Broadcom STA wireless drivers on Kali Linux

PS: This article is for only Kali Linux users, that too having a Broadcom Wireless…

1 year ago