What it does
pgrep PATTERN prints the PIDs of processes whose name matches the pattern (a regular expression). pkill PATTERN sends them a signal (TERM by default). Both select with the same options: by user (-u), terminal (-t), parent (-P), full command line (-f) and more.
Run pgrep first with the same options to see what pkill would hit.
How to write it
pgrep [OPTION]... PATTERN
pkill [OPTION]... [-SIGNAL] PATTERN
pidwait [OPTION]... PATTERNOptions 32
Most used
Signal (pkill)
Output (pgrep)
Select
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Find PIDs by name
pgrep -l sshd 1010 sshd 2210 sshd-l adds the name.
2See full command lines
pgrep -a python3Find which script is running.
3Stop all processes of a user
pkill -u olduserBefore you delete the account.
4Log out a session
pkill -KILL -t pts/3Ends everything on that terminal.
5Match the full command line
pgrep -af 'backup.sh' pkill -f 'backup.sh'The process name is bash; -f finds the script.
6Count processes
pgrep -c httpd 6Quick health check.
7Wait until a process is gone
pidwait -f backup.sh; echo "backup finished"Then start the next step.
8Reload a daemon by name
pkill -HUP rsyslogdRe-reads the config.
9The newest or oldest match
pgrep -n firefox; pgrep -o httpd-o httpd = the parent of all httpd children.
10Hunt processes that run from /tmp (security)
pgrep -af '/tmp/|/dev/shm/'Suspicious locations for running programs.
11Show what pkill did
pkill -e dd dd killed (pid 4123)Proof of the action.
Nothing matches this filter.
Watch out
Exit codes
0 | At least one process matched. |
1 | No process matched. |
2 | Syntax error. |
3 | Fatal error. |