pidof
Overview
pidof prints the PIDs of running programs matched by name. It is a quick alternative to pgrep -x for simple lookups and remains common in init scripts and one-liners. For flexible pattern matching, prefer pgrep.
Syntax
pidof [options] program [program...]Common Options
| Option | Description |
|---|---|
-s |
Single PID only (first/one) |
-c |
Only processes with same root (chroot) |
-x |
Also match scripts (by script name) |
-o omitpid |
Omit PID(s); often -o %PPID or self |
-z |
Skip zombies (when supported) |
Options vary slightly between implementations (sysvinit-utils vs others).
Examples with Explanations
Basics
pidof nginx
pidof sshd
pidof bashSingle PID
pidof -s nginx
kill -HUP "$(pidof -s nginx)"Scripts
pidof -x backup.sh
pgrep -f backup.sh # often clearer for scriptsExistence gate
if pidof dockerd >/dev/null; then
echo docker running
fiOmit PIDs
pidof -o $$ bash # other bashes, not this script’s shell if named bash
pidof -o %PPID nginxFeed to kill
kill $(pidof myapp)
# safer with pgrep:
pgrep -x myapp | xargs -r killCompare tools
pidof nginx
pgrep -x nginx
pgrep -a nginx
systemctl status nginxNotes / Pitfalls
- Name matching is not always exact across threads/workers — verify with
ps. - Multiple PIDs print space-separated — quote carefully in scripts.
- Race: process may exit between
pidofandkill. - Containers: only sees processes in the same PID namespace.
- Prefer
systemctlfor supervised services instead of raw PID kills.
2026-relevant notes
- systemd units:
systemctl show -p MainPID nginx. pgrep/pkillfrom procps are more expressive for modern scripts.- Rootless stacks may have multiple same-named processes per user — be specific.
Additional Resources
man pidof