The pidof
is a command-line tool used to find the process ID of a running program. This article shows you how to use the pidof command in Linux.
How to Use the pidof Command
pidof
is implemented differently on Red Hat and Debian based distributions. On Debian, sysvinit-utils
package includes it while on Red Hat it is a part of the procps-ng
package. We will discuss the common options.
Following is the basic syntax for the pidof
command:
pidof [OPTIONS] PROGRAM_NAME
It can take one or more names as an arguments but generally, it is used for only one name to get pidof
.
If you invoke without any arguments it will show the PIDs of all running programs that match with the given name.
For example, to find the PID of the SSH server, you would run:
pidof sshd
Here, it shows the PIDs of all running process which matchs with sshd
. If no match will found then it will be empty.
7022 6911 921
pidof
returns exit code 0
when at least running program match with given name else it will return 1
as exit code. It’s used while you writing the shell scripts.
By default, all PIDs of the matching running programs are displayed. Use the -s
option to force pidof
to display only one PID:
pidof -s program_name
Use the -o
option to exclude a process with a given PID from the command output:
pidof -o pid program_name
When use -o
option with pidof
command, you can use a special PID named %PPID
that represents the calling shell or shell script.
You can use the -c
option to return only the PIDs of the processes that are running with the same root directory.
Make sure this option can be used if you are running as root or user with sudo privileges:
sudo pidof -c pid program_name
Example Usage of the pidof Command
You can use the pidof
command in the combination with the kill command to terminate a program.
For example, if the Google Chrome browser is unresponsive, and you want to kill the Chrome processes. Find the PIDs, with pidof
:
pidof google-chrome
It will show all Chrome processes:
2520 3564 8564
Once you have the Chrome processes PIDs, send the SEGTERM
signal to terminate all of them:
sudo kill -9 2520 3564 8564
You can terminate in one command using the command substitution expression $(…)
sudo kill -9 $(pidof google-chrome)
Conclusion
You can use the pidof command to find out the PIDs of a specific running program. Generally it is used with the name of the program for which you are searching.
If you have any questions or feedback, please leave a comment below.