Linux Command Reference ยท Processes and Signals
Linux pgrep and pkill commands
Reference for pgrep and pkill: find process IDs by name or full command line, filter by user, count matches, and send signals safely with pkill.
pgrep searches the running processes and prints the IDs of those that match a pattern. pkill takes the same selection and sends a signal to every match instead (SIGTERM by default). Both are replacements for the old ps aux | grep name | awk '{print $2}' pipeline, without its flaws: they never match themselves, and they need no parsing.
The pattern is an extended regular expression matched against the process name (the first 15 characters of the command) unless you add -f, which matches the whole command line. Patterns are not anchored, so pgrep ssh also finds sshd and ssh-agent; use -x for an exact name.
pgrep -u alice -l nginx pkill -f 'python worker.py'
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
pgrep [OPTIONS] PATTERN
pkill [-SIGNAL] [OPTIONS] PATTERN
| Option | Description |
|---|---|
-f, --full |
Match against the full command line, not just the process name. |
-x, --exact |
The pattern must match the whole name (or command line with -f). |
-i |
Ignore case. |
-u USER, -U USER |
Only processes of this effective / real user. |
-g PGRP, -s SID |
Only processes in a process group / session. |
-P PPID |
Only children of the given parent. |
-t TTY |
Only processes on this terminal. |
-n, -o |
Only the newest / oldest matching process. |
-v |
Negate: select processes that do not match. |
-l, -a |
pgrep: print the name / the full command line next to each ID. |
-c |
Print only the count of matches. |
-d SEP |
pgrep: output delimiter instead of newline. |
-SIGNAL (pkill) |
Signal by name or number, for example -HUP, -9, -USR1. Default is TERM. |
-e |
pkill: print what was killed. |
-F FILE |
Take the PID from a PID file. |
Returns: 0 if at least one process matched (and for pkill the signal was sent), 1 if none matched, 2 for a syntax error, 3 for an internal error.
Usage notes
- Check before you kill. Run
pgrep -fa patternfirst and read the list. A short pattern such asjavaornodecan match more than you intend;pkill pythonhas ended many unrelated jobs. - Anchor your patterns:
-x, or-f '^/usr/bin/app\b'. Without anchorspgrep -f appalso matches your own editor onapp.conf. - Name limit: the process name is at most 15 characters, so
pgrep -x my-very-long-servicenever matches; use-ffor long names. - Only your processes can be signalled unless you are root.
pkillagainst another user's process printsOperation not permitted. - Signals: TERM asks nicely; use
-KILLonly if a process ignores TERM.-HUPis traditionally "reload your configuration" for daemons. See Kill and signals. - Race conditions: a PID can be reused between
pgrepandkill.pkillselects and signals in one step, which is safer thankill $(pgrep ...). - Use
-cfor health checks ([ "$(pgrep -xc nginx)" -ge 1 ]), andpkill -0to test existence without disturbing anything. - systemd services should be stopped with
systemctl stop, notpkill, or they may restart.