
pidof Command in Linux
The pidof command in Linux finds the process ID (PID) of a running process. It displays the PIDs to the standard output. If the process is not running, it produces no output and typically returns a non-zero exit code. It is a handy tool for Linux administrators for easy process management.
Table of Contents
Here is a comprehensive guide to the options available with the pidof command −
Syntax of pidof Command
The syntax of the pidof command is as follows −
pidof [options] [program...]
In the above syntax, the [options] field is used to specify various options such as enabling quiet mode, displaying thread IDs instead of PIDs, and others. The [program...] field is used to specify one or more processes whose PID is needed to be displayed.
pidof Command Options
The options of the Linux pidof command are listed below −
Option | Description |
---|---|
-s | Return only one PID |
-c | Show PIDs with the same root directory (root-only) |
-h | Display help text |
-n | Skip stat() calls for binaries on network file systems |
-q | Exit with success/failure without showing PIDs |
-x | Include PIDs of shells running named scripts |
-z | Detect and include zombie processes |
-d sep | Use sep as a separator for multiple PIDs |
-o omitpid | Omit processes with the specified PID (%PPID for parent shell) |
Examples of pidof Command in Linux
In this section, the usage of pidof command will be discussed with examples −
Getting PID of a Process
To get the PID of a running process, use the pidof command with the process name. For example, to find the PID of the bash process, use the following command −
pidof bash

Similarly, to get the PID of firefox program, use the following command −
pidof firefox

Firefox has multiple PIDs because it uses a multi-process architecture for improved performance, security, and stability.
Changing PID Separator
By default, the PIDs are separated by a space. To change the separator, use the -d option. For example, to change the separator to a dash (-), use the pidof command in the following way −
pidof -d '-' firefox

Getting only One PID
To display the first PID found for the firefox process, use the -s option −
pidof -s firefox

The -s option in the pidof command returns only the first PID found for the specified program. This is also known as single-shot mode, where instead of listing all PIDs of a running process, it limits the output to just one PID. It simplifies output and is useful in scripts where only one process ID is required, even if the program is running multiple instances.
Getting PID of a Running Shell Script
To display the PID of the running shell script, use the -x option −
pidof -x file.sh

Including Zombie Processes
To include the zombie processes while getting PID of a process, use the -z option −
pidof -z firefox
Zombie processes are processes that have completed execution but still have an entry in the process table. The pidof command skips these processes during execution as attempts to access the stats of these processes fail. The -z option tells pidof to try detecting sleeping and zombie processes, though it might fail or cause the command to hang.
Getting PIDs of Processes Running in the Same Root Directory
To display PIDs of the processes running in the same root directory, use the -c option −
pidof -c firefox
It is mainly used by root users to list processes that are running in the same root directory.
Omitting Specified PID
To omit the specified PID, use the -o option with the PID. For example, to display the PIDs of firefox process but omit the 8448 PID, use the following command −
pidof -o 8148 firefox

Displaying Usage Help
To display the usage help of the pidof command, use the -h option −
pidof -h
Killing a Process in Linux using pidof and kill Tools
To find the PID of an unresponsive process and kill it, both the pidof and kill command line tools are used. First, get the PID of the unresponsive process −
pidof firefox
To forcefully kill the process, use the kill command with -9 (sends SIGKILL).
sudo kill -9 7368 7362 7358 7348 7341 6870 6841 6819 6684

Conclusion
The pidof command in Linux is used to find the process ID (PID) of a running process and display it. It offers various options to customize its output, such as limiting the number of PIDs, changing separators, and detecting zombie processes.
The pidof command can be helpful for tasks like managing processes, killing unresponsive ones, and handling scripts. This tool is particularly useful for administrators to manage processes efficiently in a Linux environment.