How to find out PID in Linux

Every Linux process has a process ID (PID) assigned to it.  It is a unique identification number that is automatically assigned to each process when it is created in an operating system. A process is a running instance of a program. In this tutorial we’ll look into ways on how to find the PID of process.

Each time process ID will be getting change to all the processes except init because init is always the first process on the system and is the ancestor of all other processes. It’s PID is 1, on older SystemV systems, this is called the init process, while on newer systemd based systems, this is called the systemd proceess.

The default maximum value of PIDs is 32,768. The same has been verified by running the following command on your system cat /proc/sys/kernel/pid_max. On 32-bit systems 32768 is the maximum value. On 64-bit systems, we can set to any value up to 2^22 (approximately 4 million) . These are a lot of PID

The PIDs for the running processes on the system can be found by using the pidof command, pgrep command, ps command, and pstree command.

pidof Command

pidof used to find the process ID of a running program. It’s prints those id’s on the standard output. To demonstrate this, we are going to find out the httpd apache process id from RHEL7.

~]# pidof httpd
4458 4455 4451 4450 4449 4448 4446 4445 4441 4440 772

From the above output you may face difficulties to identify the Process ID because it’s shows all the PIDs (included Parent and Childs) aginst the process name. Hence we need to find out the parent PID (PPID), which is the one we are looking. It could be the first number. In this case it’s 4458 and it’s shorted in descending order.

pgrep Command

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout.

~]# pgrep httpd

This also similar to pidof output but it’s shorting the results in ascending order, which clearly says that the parent PID is the last one. In this case it’s 772.

Note : If you have more than one process id of the process, you may face trouble to identify the parent process id when using pidof & pgrep command.

pstree Command

pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified in the pstree command then it’s shows all the process owned by the corresponding user.

pstree visually merges identical branches by putting them in square brackets and prefixing them with the repetition count.

~]# pstree -p | grep httpd
| |-httpd(4441)
| |-httpd(4445)
| |-httpd(4446)
| |-httpd(4448)
| |-httpd(4449)
| |-httpd(4450)
| |-httpd(4451)
| |-httpd(4455)
| `-httpd(4458)

The parent process is the first one listed. pstree command is very simple because it’s segregating the Parent and Child processes separately but it’s not easy when using pidof & pgrep command.

ps Command

ps displays information about a selection of the active processes. It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). Output is unsorted by default.

~]# ps aux | grep httpd
root 772 0.0 1.2 405792 12280 ? Ss Dec20 0:03 /usr/sbin/httpd -DFOREGROUND
apache 4440 0.0 4.1 435132 42172 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4441 0.0 4.1 435124 42068 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4445 0.0 1.4 407676 14756 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4446 0.0 4.1 435104 41836 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4448 0.0 1.4 407700 14900 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4449 0.0 4.1 435104 42044 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4450 0.0 1.4 407700 14964 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4451 0.0 1.8 411260 18312 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4455 0.0 1.4 407700 14900 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
apache 4458 0.0 1.8 411260 18336 ? S 20:24 0:00 /usr/sbin/httpd -DFOREGROUND
root 4593 0.0 0.0 112660 972 pts/0 R+ 20:57 0:00 grep --color=auto httpd

From the above output we can easily identify the parent process id (PPID) based on the process start date. In this case of  httpd process was started @ Dec20 which is the parent and others are child’s. PID of apache2 is 772.

– masterkenneth

Leave a Reply

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