Linux pidof Command Tutorial for Beginners (5 Examples)

Linux command line offers a lot of utilities that work with processes. Once such tool is pidof, which – as the name suggests – gives you the process ID of an already executing process. 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 16.04 LTS machine.

Linux pidof command

As already 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 on how the tool works.

Q1. How to use pidof command?

Basic usage is fairly simple – just  pass the name of the program as input to the command. For example:

pidof gedit

How to use pidof command

So you can see the command produced gedit’s process ID in the output.

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 a web browser program as input.

pidof firefox

Following is the output produced on my system:

pidof command example

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

pidof -s firefox

pidof command single pid

Q3. How to limit output based on root directory?

If you want pidof to only return process ids that are 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 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.

Conclusion

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 majority of them here. For more info, head to the tool’s man page.

Share this page:

linux pidof command tutorial for beginners 5 examples 3
linux pidof command tutorial for beginners 5 examples 4
linux pidof command tutorial for beginners 5 examples 5
linux pidof command tutorial for beginners 5 examples 6

نوشته های مشابه