Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
pgrep -u alice -l nginx
pkill -f 'python worker.py'

Try it

Bash Find by full command line

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.

Bash Exit status and signals

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

Terminal
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 pattern first and read the list. A short pattern such as java or node can match more than you intend; pkill python has ended many unrelated jobs.
  • Anchor your patterns: -x, or -f '^/usr/bin/app\b'. Without anchors pgrep -f app also matches your own editor on app.conf.
  • Name limit: the process name is at most 15 characters, so pgrep -x my-very-long-service never matches; use -f for long names.
  • Only your processes can be signalled unless you are root. pkill against another user's process prints Operation not permitted.
  • Signals: TERM asks nicely; use -KILL only if a process ignores TERM. -HUP is traditionally "reload your configuration" for daemons. See Kill and signals.
  • Race conditions: a PID can be reused between pgrep and kill. pkill selects and signals in one step, which is safer than kill $(pgrep ...).
  • Use -c for health checks ([ "$(pgrep -xc nginx)" -ge 1 ]), and pkill -0 to test existence without disturbing anything.
  • systemd services should be stopped with systemctl stop, not pkill, or they may restart.