Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

pkill Command Guide for Killing Processes by Pattern

pkill sends a signal to every process whose name or command line matches a pattern you give it — no need to look up PIDs first.


What is pkill, and Why Use It?

The pkill command finds processes matching a pattern and signals them. It is the pattern-based sibling of kill: instead of telling it a process ID, you tell it what to look for — a name, a user, or part of a command line — and it kills every match. It is what you reach for when a program has spawned a family of workers, or when the exact name is all you know.

Why Learn pkill?

  • No PID hunting: pgrep gives you the IDs; pkill skips that step entirely and acts on the pattern directly.
  • Kills whole process families: when a program forks children (web servers, test runners), one pattern stops all of them.
  • Scriptable: patterns make kill logic readable in scripts — “kill anything named httpd” reads like English.
  • Pairs with pgrep: the two share the same pattern syntax, so knowing one teaches you the other.

pkill is the sharp end of a two-step process: find with a pattern, kill with the same pattern. It is why “the app is stuck” has a one-line answer.


1. Basic Usage: Kill by Name

# Kill every process named exactly "firefox"
pkill firefox
  • By default pkill matches the process name exactly and sends SIGTERM (15) — the polite “please exit” signal.
  • If nothing matches, pkill exits with status 1; if it killed something, status 0.

Check what would be killed first with pgrep:

pgrep -a firefox    # show the matches before killing them

2. Match Patterns, Not Just Names

# Kill anything whose name STARTS with "chrome"
pkill chrome

# Kill processes whose name contains "test"
pkill -f test
  • Plain pkill name matches the process name only.
  • -f matches against the full command line, so pkill -f "python worker" hits the whole invocation, arguments included.
FlagWhat It Matches
(none)Process name, exactly
-fFull command line
-u userProcesses owned by user
-t ttyProcesses on terminal tty
-P pidChildren of process pid
-g pgrpProcesses in process group pgrp

3. Choose the Signal

# Ask politely (default): SIGTERM
pkill firefox

# Insist: SIGKILL — the process cannot ignore it
pkill -9 firefox

# Suspend instead of kill: SIGSTOP
pkill -STOP -f "heavy-job"
  • -9 (SIGKILL) is the nuclear option: the kernel kills the process instantly, with no chance to clean up.
  • Try the default SIGTERM first; only escalate to -9 when a process refuses to die. See kill for the full signal table.
# Send SIGHUP — many daemons reload their config on this
pkill -HUP nginx

4. Narrow the Hunt

# Kill only processes owned by user "bob"
pkill -u bob -f myscript

# Kill only your own processes
pkill -u "$(id -un)" stuck-helper

# Kill only children of a specific process
pkill -P 4321
  • Combining flags keeps pkill surgical: pattern and owner and parent, all in one call.

5. Interactive and Dry-Run Modes

# Ask before each kill
pkill -i -f python

# Show what matches without killing (pgrep is the dry-run tool)
pgrep -af python
  • -i prompts kill (y/n)? for every match — useful when a pattern might catch too much.
  • pgrep -a lists the matches with their full command lines; this is the safe rehearsal for any pkill you are unsure about.

Common Pitfalls

  • pkill with no pattern kills nothing but looks like it did. Always check with pgrep -a first when unsure.
  • -f matches command lines, so patterns are risky: pkill -f vim can match vim notes.txt and any shell command containing “vim” — including your own script.
  • You can kill your own shell: a broad -f pattern can match the script running pkill itself. Test with pgrep first.
  • -9 skips cleanup: databases and daemons may leave lock files or corrupted state. SIGTERM first, always.
  • Patterns are regexes, not glob names: pkill firefox* does not mean “firefox and friends” the way a shell glob does — anchor it: pkill '^firefox$'.
  • Exit codes matter in scripts: pkill returns 1 when nothing matched; don’t let that abort a script unexpectedly.

PageRelationship
pgrepFinds the processes pkill would kill — the safe rehearsal.
killThe PID-based cousin; same signals, manual targeting.
killallKills by exact name; pkill adds pattern matching.
psShows the processes before you match them.
topLive view; spot what needs killing.

pkill Cheatsheet

CommandWhat It Does
pkill nameKill processes named name (SIGTERM).
pkill -9 nameForce-kill with SIGKILL.
pkill -f patternMatch the full command line.
pkill -u user patKill only user’s matches.
pkill -P pidKill children of pid.
pkill -HUP daemonSignal a daemon to reload config.
pkill -i patAsk before each kill.
pgrep -af patPreview the matches first.