In Linux, a process is a task that our machine is currently working on. The initial process is referred to as the parent process while any other process originating from a parent is known as a child process. Additionally, a process is identifiable through its process ID (PID). Therefore, it’s possible to get the parent process ID (PPID) of a child process.
In this tutorial, we’ll focus ps command and learn how to get the parent process ID of a child process. The command contains a variety of options that we’ll discuss.
2. Using the ps Command
ps is a commonly used command for checking PIDs. It stands for “process status”. We’ll use it to display information related to the currently running processes.
First, we’ll pass the -e option to the ps command to list all the running processes:
$ ps -e PID TTY TIME CMD 1 ? 00:00:03 systemd 2 ? 00:00:00 kthreadd .... 2987 ? 00:00:00 chrome 3010 ? 00:08:06 Xwayland
At this point, we get the process ID (PID), terminal line (TTY), run time (TIME), and command (CMD).
Next, we’ll filter out a process from the output above by its process name by using the pgrep command. pgrep lists the currently running processes according to the search criteria supplied to it.
Let’s pass the -l option to pgrep to list the process name as well as the process ID:
$ pgrep -l chrome 2961 chrome 2974 chrome_crashpad
In this example, we passed the process name “chrome” as the search criteria and got its process ID.
At this point, we can use the process ID of the process we got above to get the parent process ID:
$ ps -o ppid= -p 2961 2129
In this case, after passing the parent ID to the command above, we get its parent process ID which is 2129.
In this article, we discussed an easy and efficient way to get the parent process ID in Linux using the ps and pgrep commands.
We also discussed some of the options these commands provide. Both these commands contain the –help option if we need additional information.