Categories
Linux MAC UNIX

How do you find the parent process? (ps and pstree)

Finding the parent process is pretty simple. Lets say that you do a top and see that httpd has a pid of 3027.

Use ps

You can easily find its parent process by using the following command(s).

$ ps aux -o ppid | grep 3027 | grep -v grep
_www 3027 0.0 0.0 2438040 616 ?? S 8:01PM 0:00.00 /usr/sbin/httpd 3001

$ ps -l 3027 | grep -v grep
 UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD
 70 3027 3001 104 0 31 0 2438040 616 - S eada7e0 ?? 0:00.00 /usr/sbin/httpd -D FOREGROUND

As you can see, 3001 is the parent process id of 3027.

Use pstree

As you can see, 3001 is the parent process id of 3027.

$ pstree -p 3027
-+= 00001 root /sbin/launchd
\-+= 03001 root /usr/sbin/httpd -D FOREGROUND
\--- 03027 _www /usr/sbin/httpd -D FOREGROUND

If you do not have pstree already installed, you can download and install it by following the below directions.

IMPORTANT NOTE: If you are using a MAC, you can download pstree directly HERE. Change (cd) into the pstree directory and skip to the “build, test and install” step below.

# make working directory

$ mkdir pstree
$ cd pstree

# fetch source, requires wget

$ wget http://www.thp.uni-duisburg.de/pstree/pstree.tar.gz
$ tar xzf pstree.tar.gz

# build, test and install

$ gcc -O -o pstree pstree.c
$ ./pstree
$ sudo mv pstree /usr/bin/
Comments

By Tommy Elmesewdy

DevOps Engineer

2 replies on “How do you find the parent process? (ps and pstree)”