Pidof Command in Linux

Posted 

3 min read

pidof is a command-line utility that allows you to find the process ID of a running program.

In this article, we will explain how to use the Linux pidof command.

How to Use the pidof Command

There are different implementations of pidof for Red Hat and Debian based distributions. On Red Hat distributions, the pidof command is a part of the procps-ng package, while on Debian, it is part of sysvinit-utils. We’ll go over the options that are common for both implementations.

The syntax for the pidof command is as follows:

pidof [OPTIONS] PROGRAM_NAME

The command accepts zero or more names as arguments, but typically, you would pass only one name to pidof.

When invoked without any option, pidof will print 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

If there are running processes with names matching sshd, their PIDs will be displayed on the screen. If no matches are found, the output will be empty.

4382 4368 811

pidof returns 0 when at least one running program matches with the requested name. Otherwise, the exit code is 1. This can be useful when writing shell scripts.

To be sure that only the PIDs of the program you are searching for are displayed, use the full pathname to the program as an argument. For example, if you have two running programs with the same name located in two different directories pidof will show PIDs of both running programs.

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

The -o option allows you to exclude a process with a given PID from the command output:

pidof -o pid program_name

When pidof is invoked with the -o option, you can use a special PID named %PPID that represents the calling shell or shell script.

To return only the PIDs of the processes that are running with the same root directory, use the -c option.

This option works only pidof is run as root or sudo user:

pidof -c pid program_name

Example Usage of the pidof Command

The following example shows how to use the pidof command in combination with the kill command to terminate a program.

Let’s say the Firefox browser has become unresponsive, and you need to kill the Firefox processes. First, find the PIDs, with pidof:

pidof firefox

The command will print all Firefox processes:

2551 2514 1963 1856 1771

Once you know the Firefox processes PIDs, send the SEGTERM signal to terminate all of them:

sudo kill -9 2551 2514 1963 1856 1771

You can also use the command substitution expression $(...), to terminate the program in one command:

sudo kill -9 $(pidof firefox)

Conclusion

The pidof command is used to find out the PIDs of a specific running program.

pidof is a simple command that doesn’t have a lot of options. Typically you will invoke pidof only with the name of the program you are searching for.

If you have any questions or feedback, feel free to leave a comment.