find parent process of given process

How to Find Parent Process ID in Linux

Sometimes you may need to find parent process ID of a process in Linux. This is required if your process is spawned from another process, like a server, and you want to find its parent process to kill it. You can easily do this using PPID command. In this article, we will look at how to find parent process ID in Linux.


How to Find Parent Process ID in Linux

Here are the different ways to find parent process ID in Linux.

Let us say you want to find parent process id of process with ID 4079, then you can run the following ps command to find its parent process.

$ ps -o ppid=4079
2381

You may also use ps -f command as shown, to get detailed information about the process, along with its process id.

$ ps -f 4079
UID        PID  PPID  C STIME TTY      STAT   TIME CMD
izx       4079  2381  0 07:16 ?        S      0:00 /usr/lib/pulseaudio/pulse/gconf-helper

If you want to view parent process of your environment variable, terminal, then you can simply echo $PPID.

$ sudo echo $PPID

If you want to view the entire process tree, you can use the pstree command instead.

$ sudo pstree -s -p 4079
init(1)───apache2(2381)───apache2(4079)

The is useful if your parent process has multiple child processes and you want to find out all child processes that belong to your parent process.

Similarly, you can get pstree for environment variable using $PPID variable.

$ sudo pstree $PPID

In this article, we have looked at how to easily get parent process of a given process.

Sometimes you may need to find out the parent process ID or sibling process ID of your existing process, in case it is not working properly or you want to kill it. In such cases, you may be unable to stop these services using systemd or init, and the only way to proceed might be to find their PIDs and issue kill command. This is especially true if you run servers like Apache, NGINX, etc.

Using ps and pstree commands, you will be able to get process IDs of parent process and stop them if are facing any issues with them.

It may also happen that your system has some orphan processes without any parent and you want to identify them. Even in such cases, the above approach can be used to determine if a process has parent or not. Orphan process have parent process ID as 1 (init process).

Also read:

How to Change Default Shell in Linux
How to Install Moodle with NGINX in Ubuntu
How to Download File in Python
How to Run CGI Script in Apache
How to Create Permanent Alias in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *