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
| Option | What it does |
|---|---|
| (no options) | the pattern is searched in the process name: pgrep ssh finds both sshd and ssh-agent |
-x | the name must match exactly: pgrep -x sshd |
-f | search the full command line (with arguments and paths) |
-i | case-insensitive |
-v | invert: all processes that do not match |
Filters
| Option | What it selects |
|---|---|
-u user | by effective user (name or UID, comma-separated) |
-U user | by real user |
-G group | by real group |
-P PPID | only children of the given process |
-g PGID / -s SID | by process group / session |
-t pts/0 | by terminal |
-r S,D | by state (R, S, D, Z, T — as in ps) |
-n / -o | only the newest / oldest of the matches |
-O sec | only processes older than the given number of seconds |
-F file | take the PID from a PID file |
-A | exclude your own ancestors (the shell pgrep was started from) |
pgrep output
| Option | What it prints |
|---|---|
| (no options) | PIDs, one per line |
-l | PID and name |
-a | PID and full command line |
-c | only the count |
-d , | delimiter instead of newline: 12,34,56 |
-w | thread IDs (TID) instead of PIDs |
pkill options
| Option | What it does |
|---|---|
-HUP, -9, --signal TERM | which signal to send |
-e | print each process that was signaled: … killed (pid 42) |
-c | print 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_sleeperfinds nothing (and warns about it). Use-for the first 15 characters. - Without
-xthe pattern is a substring.pkill sshkills bothsshdand your own ssh session. Withpkill, almost always add-xor a precise-f. - The pattern is a regex. A dot means "any character",
+and(are special.pgrep -f 'app.py'also findsappXpy. -fmatches other people's command lines too: an editor that opened the fileworker.confis found by the patternworker. Make the pattern more specific.- Self-matching with the shell — see above: in scripts and multi-command
bash -c, use[x]yzor-A. pkillwithout a signal sendsTERM, notKILL. The process can catch it and not exit.