pidof
Find process ID(s) by name
TLDR
List all process IDs with given name
List a single process ID with given name
List process IDs including scripts with given name
Kill all processes with given name
SYNOPSIS
pidof [-s] [-c] [-x] [-o omitpid] [-q] program ...
PARAMETERS
program ...
One or more program names for which to find PIDs. pidof typically matches the exact command name (argv[0]) of the running process, which includes its path if specified.
-s
(Single shot) Return only one PID, even if multiple instances of the program are running. The specific PID returned may vary and is not guaranteed to be the oldest or newest.
-c
(Comm mode) Search for processes whose command name (as found in the comm field of /proc/PID/stat) matches the given program. The comm field is typically truncated to 15 characters, which can make this option more restrictive than the default argv[0] matching.
-x
(Scripts) Also return PIDs of processes that are running a shell script specified by name. This is useful for identifying running instances of scripts that are executed via an interpreter (e.g., bash script.sh).
-o omitpid
(Omit PID) Omit specific PIDs from the output. omitpid can be a single PID or a comma-separated list of PIDs.
-q
(Quiet) Suppress any error messages from being displayed. Only the PIDs will be outputted to standard output.
DESCRIPTION
The pidof command finds the process IDs (PIDs) of one or more running programs. It takes program names as arguments and returns a space-separated list of PIDs corresponding to those programs.
Unlike pgrep, which uses regular expressions and can search various process attributes, pidof primarily matches the exact command name (argv[0]) of the running executable. This makes it particularly useful in shell scripts where you need to quickly obtain the PID of a known process for further action, such as sending a signal with kill.
CAVEATS
pidof relies on the /proc filesystem for process information. It is generally less flexible than pgrep as it doesn't support regular expressions for matching program names. When multiple instances of a program are running, pidof will list all their PIDs unless the -s option is used. The default matching behavior is on argv[0], which might include the full path to the executable (e.g., /usr/bin/nginx).
EXIT STATUS
pidof returns an exit status of 0 if one or more PIDs are found for the specified programs, and a non-zero status (typically 1) if no PIDs are found or an error occurs. This makes it suitable for conditional execution in shell scripts.
USE CASES
Commonly used in shell scripts to check if a process is running, to obtain a PID for sending signals (e.g., stopping a service), or for managing daemon processes. For example:
if pidof myserver > /dev/null; then echo "myserver is running"; fi
HISTORY
pidof has been a standard utility on many Linux distributions. On some systems, particularly those using sysvinit-utils, pidof is often implemented as a symbolic link to the killall5 command. While their primary functions differ (killall5 typically sends signals to all processes except kernel processes and its own children), this historical link reflects its common use in system initialization scripts and process management.