← Full reference

pgrep and pkill

Find processes by name or command line and send them a signal without ps | grep

pgrep searches for processes by a pattern and prints their PIDs. pkill takes the same conditions, but instead of printing it sends the matching processes a signal (TERM by default). Both replace the ps aux | grep … | awk … | xargs kill chain and never match themselves.

Syntax

pgrep [options] pattern
pkill [-signal] [options] pattern

The pattern is an extended regular expression (ERE), and by default it is matched as a substring of the process name (comm, up to 15 characters). With -f — against the whole command line.

Exit code: 0 — something found, 1 — nothing, 2 — error in options.

How to match

OptionWhat it does
(no options)the pattern is searched in the process name: pgrep ssh finds both sshd and ssh-agent
-xthe name must match exactly: pgrep -x sshd
-fsearch the full command line (with arguments and paths)
-icase-insensitive
-vinvert: all processes that do not match

Filters

OptionWhat it selects
-u userby effective user (name or UID, comma-separated)
-U userby real user
-G groupby real group
-P PPIDonly children of the given process
-g PGID / -s SIDby process group / session
-t pts/0by terminal
-r S,Dby state (R, S, D, Z, T — as in ps)
-n / -oonly the newest / oldest of the matches
-O seconly processes older than the given number of seconds
-F filetake the PID from a PID file
-Aexclude your own ancestors (the shell pgrep was started from)

pgrep output

OptionWhat it prints
(no options)PIDs, one per line
-lPID and name
-aPID and full command line
-conly the count
-d ,delimiter instead of newline: 12,34,56
-wthread IDs (TID) instead of PIDs

pkill options

OptionWhat it does
-HUP, -9, --signal TERMwhich signal to send
-eprint each process that was signaled: … killed (pid 42)
-cprint how many processes were signaled
all pgrep filters-f, -x, -u, -P, -n, -o…

Examples

# PIDs of processes named exactly nginx
pgrep -x nginx

# with full command lines
pgrep -a python3

# by arguments: a process started with a specific config
pgrep -af 'worker --queue=mail'

# how many processes a user has
pgrep -c -u www-data

# the newest ssh process
pgrep -n -x sshd

# comma-separated PIDs — for ps -p
ps -o pid,etime,cmd -p "$(pgrep -d, -x cron)"

# is a process running — in a condition
if pgrep -x cron >/dev/null; then echo "cron is running"; fi

# stopped (T) processes
pgrep -a -r T

# make all processes with a name re-read their config
pkill -HUP -x nginx

# terminate a user's processes and show which ones
pkill -e -u alice

# kill by command line
pkill -f 'sleep 1000'

-f self-matching and the bracket trick

pgrep -f looks at the command lines of all processes. If pgrep is started from bash -c '…' (or from a script), that shell's command line also contains the pattern — and pgrep finds it. pgrep excludes itself, but not its parent shell:

bash -c "pgrep -af 'sleep 777'; true"
# 9 bash -c pgrep -af 'sleep 777'; true     ← there is no sleep 777 process, yet it was "found"

This way a "process is running" check always succeeds, and a "process is stopped" check always fails. The fix is square brackets in the pattern:

pgrep -f '[s]leep 777'

The regex [s]leep matches the string sleep, but the shell's command line contains [s]leep — which no longer matches itself. The same trick works with ps aux | grep '[n]ginx'. Another option is pgrep -A, which skips all of pgrep's ancestors.

Pitfalls

  • A process name is at most 15 characters. pgrep averyverylongname_sleeper finds nothing (and warns about it). Use -f or the first 15 characters.
  • Without -x the pattern is a substring. pkill ssh kills both sshd and your own ssh session. With pkill, almost always add -x or a precise -f.
  • The pattern is a regex. A dot means "any character", + and ( are special. pgrep -f 'app.py' also finds appXpy.
  • -f matches other people's command lines too: an editor that opened the file worker.conf is found by the pattern worker. Make the pattern more specific.
  • Self-matching with the shell — see above: in scripts and multi-command bash -c, use [x]yz or -A.
  • pkill without a signal sends TERM, not KILL. The process can catch it and not exit.

See also

ps · kill · regex · proc