Using Linux Pstree to display process parent-child relationship
Updated: Jun 20
Pstree displays the process parent-child relationship in a hierarchical format on Linux. We can use this command to find the parent or child process easily. The output of this command is quite similar to the output of the ps axjf command and the ps -ef --forest command. But pstree result is much easier to understand. Let us start.
Pstree options on Linux
The commonly used options in pstree command are -a -s -p. This will tell us all the related processes. For more options, please check man pstree.
-a Show command line arguments. -s Show parent processes of the specified process. -p Show PIDs.
Use pstree to display the process parent-child relationship
Let us look at an example for this. Now we want to know what is our parent or child process id after we login to our server.
First, we need to get the current shell process id with the command: echo $$
$ echo $$ 715
Second, we can get the parent process id with pstree -asp pid
$ pstree -asp 715 systemd,1 --switched-root --system --deserialize 22 `-sshd,1680 -D `-sshd,519 `-sshd,714 `-bash,715 `-pstree,18031 -asp 715
From the output, we can easily see that the parent process id for 715 is 714. The child process id is 18031.
If we want to get all the child processes ids for one specific process, we can also use this command.
bash-4.2$ pstree -asp 519 systemd,1 --switched-root --system --deserialize 22 `-sshd,1680 -D `-sshd,519 `-sshd,714 `-bash,715 `-bash,20928 `-pstree,21090 -asp 519