System and processes

The pidof command

Returns the PIDs of a program by its exact name, for use in scripts.

Syntax

pidof [-s] [-c] [-q] [-x] [-o PID] [-d SEP] programa...Try it in the terminal →

Most useful options

-sReturns only one PID even when there are several instances of the program
-xAlso includes the shells that run a script with that name
-qPrints nothing: it only leaves the exit status, 0 if the program is found and 1 if not
-o <PID>Omits that PID from the result. With %PPID it omits the process that calls pidof
-d <separador>Separator between PIDs when there are several. A space by default
-cOnly PIDs with the same root directory. For regular users it is ignored

Copy-ready examples

$ pidof sshdPID of the SSH server. If there is none, it prints nothing.

Example output

634
$ pidof -s basha single PID, useful when several shells are open and you only want one.

Example output

842
$ pidof -q sshd && echo activochecks whether the program is running, without printing any PID.
$ pidof -o %PPID -x mi-script.shlooks for the script without counting the shell you launched it from.
$ kill -HUP $(pidof -s sshd)sends SIGHUP to the first sshd found. Destructive if the program does not ignore it: check with pidof first.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: pidof: command not foundpidof comes in the sysvinit-utils package, which is missing from minimal images and containers.Install it with sudo apt install sysvinit-utils on Debian.

Typical mistakes

  • With no matches it prints nothing and exits with code 1: in a script, check the exit code or use -q.
  • It matches the executable name, not the command line: a Python script shows up as python3. For the full line, use pgrep -f.
  • It skips zombie processes and those waiting on a disk (states Z and D). The -z option tries to detect them, at the risk of hanging.

Differences from similar commands

  • pgrep: Searches by pattern and can show the name with -l. pidof requires the exact program name.
  • ps: Lists processes with their columns. pidof only returns the PIDs of a program.
  • pstree: Draws the process tree. pidof returns only the numbers.

Related commands

FAQ

How do I store the PID in a variable?

With pid=$(pidof -s sshd). With -s the variable gets a single line even when there are several instances.

How do I stop pidof from finding the script that calls it?

With -o %PPID, which omits the parent process of pidof: the shell or script that runs it.

What happens with zombie or stuck processes?

pidof skips them by default because querying them can fail or hang. The -z option tries to detect them, accepting that risk.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course