How to Use the pidof Command to Find Process IDs in Linux

The Need for Process IDs

In a Linux operating system, every single running program, script, and background service is assigned a unique numerical identifier called a Process ID (PID). Whenever you need to perform an administrative action on a running program—such as forcefully terminating it with the kill command, changing its CPU priority with renice, or attaching a debugger like strace—you must know its exact PID.

The traditional way to find a PID is to run the ps aux command and pipe it into grep (e.g., ps aux | grep nginx), and then visually scan the output columns for the correct number. This is tedious for humans and highly prone to error if you are writing automated bash scripts.

The pidof command was created to solve this. It is a highly focused, single-purpose utility that searches for a running program by its exact name and outputs nothing but its raw numerical Process ID.

Step 1: Finding a Basic Process ID

To use the command, simply type pidof followed by the exact name of the executable you are looking for.

For example, if you want to find the PID for the Apache web server daemon, you would run:

pidof apache2

The terminal will instantly output a number (e.g., 4092). There are no headers, no columns, and no extra text. Because the output is so clean, you can easily embed it into a script (e.g., kill $(pidof apache2)).

Step 2: Handling Multiple PIDs

Many modern applications do not run as a single process. Web browsers like Google Chrome and web servers like Nginx use a multi-process architecture. They spawn one “master” process and dozens of “worker” processes to handle heavy loads.

If you run pidof against a multi-process application, it will return a space-separated list of every single PID associated with that name.

pidof nginx

Output:
14302 14301 14300 14299 1105

If you feed this string of numbers into a kill command, it will successfully terminate every single worker process and the master process simultaneously.

Step 3: Returning Only the Single Output (-s flag)

If you are writing a script that only expects to deal with one single Process ID, feeding it five PIDs at once will cause the script to crash with a syntax error.

To force pidof to return only one single PID (usually the master process or the most recently spawned thread), use the -s (single shot) flag.

pidof -s nginx

Output:
1105

This guarantees that your script will always receive exactly one integer, preventing unexpected crashes.

Step 4: Excluding Specific Scripts (-x flag)

A common mistake when using pidof occurs when you are running a bash script that happens to have the exact same name as the program you are looking for.

For example, if you wrote a script called apache2.sh that manages the server, running pidof apache2 might return the PID of the web server and the PID of your bash script. If you pass those numbers to a kill command, your script will accidentally commit suicide.

To prevent pidof from returning the PIDs of shell scripts, use the -x flag.

pidof -x apache2

This forces the command to only return the PIDs of actual binary executables, ensuring your automated server management scripts run safely and predictably.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.