Linux pidof Command Tutorial for Beginners (5 Examples)

Linux command line offers a lot of utilities that work with processes. The pidof command in Linux is a utility that identifies the process ID (PID) of one or more running processes. It is sometimes necessary to know a process' PID in order to interact with it using other commands; pidof makes the task easier by allowing you to search for a process by its name and quickly get back its associated PID. pidof is usually included with most distributions of Linux, and it can be used from the command line.

In this tutorial, we will discuss the basics of pidof using some easy-to-understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 22.04 LTS machine.

Linux pidof command

As mentioned above, the pidof command lets you find the process ID of a running program. Following is its syntax:

pidof [options] command

And here's what the man page says about the tool:

       Pidof finds the process id's (pids) of the named  programs.  It  prints
       those id's on the standard output. This program is on some systems used
       in run-level change scripts, especially when the system has a  System-V
       like   rc  structure.  In  that  case  these  scripts  are  located  in
       /etc/rc?.d, where ? is the runlevel. If the system  has  a  start-stop-
       daemon (8) program that should be used instead.

Following are some Q&A-styled examples that should give you a good idea of how the tool works.

Q1. How to use pidof command?

Basic usage is fairly simple - just pass the program's name as input to the command. For example:

pidof nano

So you can see the command returned nano's process ID in the output.

The pidof command

Q2. How to make pidof return only single pid?

Sometimes, you'll see the pidof command returns multiple pids. For example, try running the pidof command with ssh daemon as input.

pidof sshd

Following is the output produced on my system:

Multiple pid of a Linux program

And here in comparison, how the processes get listed by the ps command:

process ID in ps command

However, you can force the tool only to produce a single PID in output. This can be done using the -s command line option.

pidof -s sshd

Return a single process ID

Q3. How to limit output based on root directory?

If you want pidof only to return process ids running with the same root directory, use the -c command line option.

pidof -c program_name

Please note that this option is ignored for non-root users, as they will be unable to check the current root directory of processes
they do not own.

Q4. How to use pidof in case of binaries kept on network file systems?

While dealing with binaries located on network-based file systems like NFS, you can use the -n command line option to make pidof command avoid using the stat system function call.

pidof -n program_name

Please note that the man page suggests, "instead of using this option, the variable PIDOF_NETFS may be set and exported."

Q5. How pidof works internally?

Official docs say pidof is actually the same program as killall. The program behaves according to the name under which it is called. Here's what the man page exactly says:

       When pidof is invoked with a full pathname to the program it should
       find the pid of, it is reasonably safe. Otherwise it is  possible  that
       it  returns  pids of running programs that happen to have the same name
       as the program you're after but are actually other programs. Note  that
       that  the executable name of running processes is calculated with read?
       link(2), so symbolic links to executables will also match.


The pidof command may come in handy in many situations. For example, if you want to kill a process whose PID you don't know, you can quickly use this tool to find the process id, and the process can be killed within no time. The pidof command doesn't offer many options, and we've already discussed most of them here. For more info, head to the tool's man page.

Share this page:

Suggested articles

0 Comment(s)

Add comment