Using the pidof Command on Linux

In this tutorial, you will learn how to use the pidof command on a Linux system.

pidof command on Linux

The pidof command is a unique utility that gives you the ability to quickly and easily find the process ID of any running program.

This tool is especially useful when you need to find a processes ID quickly but only know the name of the process.

There is a downside to this tool in that if you don’t provide an absolute path for a process, it will pick up any process with the name you specified. So, for example, if you have two different programs that happen to use the name “firefox“, it would pick up process IDs for both programs.

Over the following sections, we will explore the various ways to use the pidof command.

Table of Contents

Syntax of the pidof Command

Below you can see the syntax for the pidof command. It is simply “pidof” followed by any options, then finally the filename.

pidof [OPTIONS] PROCESS_NAME

The “[OPTIONS]” parameter allows you to control the behavior of the pidof utility. For example, using these options, you can modify how the utility looks for processes and the IDs it returns. This parameter is completely optional.

Whenever you call “pidof“, you will need to specify a process name (PROCESS_NAME). The tool will use this name when it searches for the process IDs. You can specify multiple process names, but typically you will want to stick with one.

Basic Usage of the pidof Command

For the most basic usage of the pidof command, you only need to specify the process name.

pidof PROCESS_NAME

When you invoke the utility in this way, pidof will find all IDs belonging to any process utilizing the specified name. As you can run any program multiple times, you can potentially end up with multiple process IDs.

Please note that this command will not return the process IDs of shells running the named scripts without using an option.

Example Basic Usage of the pidof Command

To show you how the pidof command works, we will find all process IDs belonging to a process with the name “sshd“.

To achieve this, we need to call “pidof” followed by the process name “sshd“.

pidof sshd

If any processes are running under that name, you will get a list of their IDs, as shown below.

17393 17385 16846 16838 637

Only Returning a Single Process ID with the pidof Command

By default the pidof command will return as many process IDs as it can find for your chosen process name. However, the utility allows you to control this behavior by using one of its options.

To tell pidof only to fetch a single process ID, all you need to do is use the tools “-s” option.

pidof -s PROCESS_NAME

Instead of returning multiple process IDs, the pidof tool will stop on the first process it finds and return the ID.

Example of Returning a Single Process ID

For this example, we will use pidof to get a single process ID for our process called “sshd”. We have multiple SSH clients open, so we know that this would typically return multiple process IDs.

All we need to do is use “pidof“, followed by the “-s” option, and then the process name which is “sshd“.

pidof -s sshd

Below, you can see that we only got a single PID despite having multiple processes running.

17393

Excluding a Process ID from the pidof Output

If there is a particular process ID you want to be excluded, the pidof command has an option to allow you to omit it from the result.

You can use this omit by using “-o” and specifying the process ID afterward. The option and its value needs to be separated by a single space.

pidof -o PID PROCES_NAME

You can exclude multiple process IDs by separating each ID by a comma (,).

Example of Excluding a Process ID

To show how this works, we will get the process IDs belonging to the “sshd” program. However, we will exclude the ID “1793” from this output.

We need to use “pidof” followed by the “-n” option, the process ID we want to exclude (17393), and finally, the process name (sshd).

pidof -n 17393 sshd

We get all of the process IDs belonging to “sshd” using this command. This result will have the ID we specified automatically excluded.

17385 16846 16838 637

Using the pidof Command to get the Process ID of a Script

By default, the pidof command will not return the process IDs if it belongs to a script. However, pidof has an option that allows you to modify this behavior.

By using the “-x” option with pidof you can tell it to include searches as part of its process lookup.

pidof -x PROCESS_NAME

Example of Getting the Process ID of a Script

For this example, we created a script called “examplescript.sh“. Within this script, we wrote a simple while loop that prints out the word “Helloevery 10 seconds.

This script was given execute privileges then ran. We can now use the pidof command to find the PID of the script.

All we need to do is use “pidof“, followed by the "-x” option, and finally, the script name, which is “examplescript.sh“.

pidof -x examplescript.sh

Thanks to us using the “-x” option, you can see that the pidof tool managed to find the process ID belonging to our script.

22825

Modifying the Separator of the pidof Command

The pidof command allows you to control the separator it uses within its output. The tool will use a single space to separate each process ID by default.

This option is relatively simple to use. First, you need to use the “-d” option followed by the separator you want to use.

pidof -d SEPARATOR PROCESS_NAME

Example of Changing the Separator of the pidof Command

For this example, we will change the separator of the pidof command from a single space, to a comma (,).

We will be using “sshd” for our process name as we know we have multiple instances running.

pidof -d , sshd

After running this command, you will now see a result as shown below. In addition, instead of spaces separating the results, you should now see commas.

17393,17385,16846,16838,637

Include Stuck Process IDs in the pidof Output

By default, the pidof command will not include any processes stuck in an uninterruptible or zombie state. However, you can find processes in this state using tools like the top command.

The utility ignores these processes because they can cause pidof to hang.

To include these results in the search, you will need to use the “-z” option.

pidof -z PROCESS_NAME

Example of Including Stuck Process IDs

We will be using the “-z” option on our “sshd” process for this example.

Thanks to using this option, we should now get a list of processes, including ones in a stuck state.

pidof -z sshd

Below is an example of the data you will see within your terminal after running this command.

23573 23564 17393 17385 16846 16838 637

Return Process IDs Running in the Same Root Directory

You can tell the pidof command that it should only return the process IDs that exist within the same root directory.

To use this functionality, you need to use the “-c” option. Please note that you must run this command as either the root user or a user with superuser privileges (Use sudo).

pidof -c PROCESS_NAME

Example of Returning Process IDs in the Same Root Directory

For this example, we need the name of a process you want to get the process IDs for.

All we need to use is “pidof“, followed by the “-c” option, and then the process name “sshd“.

sudo pidof -c sshd

Using this result, you will get a list of process IDs belonging to the process named “sshd“. The command will only list processes that exist within the same root directory.

Don’t use stat on Binaries Located on a Network Filesystem

By default, when you use the pidof command, it will perform an internal call to the stat command on all binaries. However, the tool allows you to stop it utilizing this when dealing with a binary located on a network-based filesystem such as NFS.

To tell the utility not to perform this stat call, you can utilize the “-n” option alongside “pidof” and a process name.

pidof -n PROCESS_NAME

Example of Not using stat on Networked Binaries

You will need to have a binary installed on a networked filesystem to show how this works. In our case, we have a binary for “gimp” being run off a networked drive for testing.

All we need to do is use “pidof“, the “-n” option, and finally the process name, which is “gimp“.

pidof -n gimp

Using this option, you should still get a list of process IDs for that particular process name.

28513

Conclusion

This guide will have walked you through the different ways to use the pidof command on your Linux system.

pidof is an incredibly helpful tool that allows you to get the process ID by only using the process name.

There are a wide variety of ways that you can control this tool that we have explored thoroughly.

Please comment below if you have run into any issues using the pidof command.

Be sure to check out our many other Linux command guides and our Linux tutorials.

Leave a Reply

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