pkill

On this page 7

pkill is a command-line utility that sends a signal to processes selected by pattern rather than by process ID, matching against the process name, the full command line, the owner, the terminal or other attributes. Among the tools for managing processes in Linux, it combines the selection logic of pgrep with the delivery mechanism of kill, so a single command such as pkill nginx can locate and signal every matching process at once.

The utility ships in the procps-ng package on most distributions and reads process information from the /proc filesystem. By default it sends SIGTERM (signal 15), which asks a process to shut down cleanly, but any Unix signal can be specified. Because pattern matching can catch more processes than intended, pkill rewards precision and punishes careless patterns.

How pkill selects and signals processes

pkill works in two stages: it builds a candidate list from /proc, then delivers one signal to each process that passes every selection criterion. For each numeric directory under /proc, it reads the process name from /proc/PID/stat or /proc/PID/comm and, when requested, the full argument list from /proc/PID/cmdline.

The pattern is an extended regular expression, not a shell glob, and it is matched against the process name unless the -f option is given. Matching is unanchored by default, so pkill ssh also matches sshd and ssh-agent. All selection options combine with logical AND: pkill -u alice -t pts/2 vim signals only vim processes owned by alice on that terminal.

After selection, pkill calls the kill() system call for each process. The kernel then checks permissions: an unprivileged user can signal only processes with a matching user ID, while root, or a process holding the CAP_KILL capability, can signal any process. pkill never signals itself, and it exits with a status code that reports the outcome.

The following table lists the exit status codes that scripts commonly test after running pkill.

Exit codeMeaningTypical script reaction
0At least one process matched and was signaledContinue normally
1No process matched the criteriaTreat as already stopped
2Syntax error in the command lineFix the invocation
3Fatal error such as memory exhaustionAbort and investigate
Exit status codes returned by pkill

Key options and matching criteria

pkill accepts a set of selection options that narrow the candidate list far beyond the bare process name. Choosing the right options is the main defense against signaling the wrong process, especially on multi-user servers or hosts running containers.

OptionEffectExample
-fMatches the full command linepkill -f ‘python3 worker.py’
-xRequires an exact name matchpkill -x sshd
-u / -UFilters by effective or real userpkill -u alice firefox
-tFilters by controlling terminalpkill -t pts/3
-PSelects children of a parent PIDpkill -P 1234
-n / -oSelects only the newest or oldest matchpkill -n chrome
-ePrints each process that is signaledpkill -e -f backup
–signalSets the signal to sendpkill –signal HUP rsyslogd
Frequently used pkill options

One frequent trap is the 15-character name limit. The kernel stores the process name in the comm field, which is truncated to 15 characters, so a long executable name will not match unless the pattern is shortened or -f is used. Recent procps-ng versions warn when a pattern exceeds this limit.

The signal can be given as -SIGNAL, -NUMBER or --signal NAME. Modern versions also accept -q to send a real-time signal with a value and --ns with --nslist to restrict matching to processes in the same Linux namespace as a reference PID, which helps on container hosts.

pkill compared with kill, killall and pgrep

pkill belongs to a family of signal tools that differ mainly in how they identify targets. kill requires explicit PIDs, killall matches exact names, pgrep only lists matches, and pkill signals pattern matches directly.

ToolTarget selectionSends signalsMain risk
killExplicit PID or job numberYesStale or mistyped PID
killallExact process name by defaultYesDifferent behavior on other Unix systems
pgrepPattern and attributesNo, lists PIDsNone, read-only
pkillPattern and attributesYesOverly broad pattern
Comparison of process signaling tools in Linux

The most reliable workflow is to run pgrep first with identical arguments, for example pgrep -a -f worker.py, review the listed processes, and only then replace pgrep with pkill. Because both commands share the same parser, a dry run with pgrep previews exactly what pkill will signal. On some commercial Unix systems, killall without arguments terminates all processes, which makes pkill the safer portable habit.

Choosing the right signal

The signal determines what the target process is asked to do, and the default SIGTERM is not always the best choice. Well-written daemons install handlers for SIGTERM and SIGHUP, while SIGKILL cannot be caught or ignored and gives the process no chance to flush buffers, remove lock files or close network connections.

SignalNumberTypical effectWhen to use it
SIGTERM15Requests graceful shutdownDefault first attempt
SIGHUP1Reloads configuration in many daemonsApplying config changes
SIGINT2Interrupts, like pressing Ctrl+CStopping foreground tools
SIGSTOP19Pauses the process unconditionallyFreezing a runaway job
SIGCONT18Resumes a stopped processAfter SIGSTOP
SIGKILL9Terminates immediately in the kernelLast resort only
Common signals used with pkill

Signal numbers above are for x86 and ARM; some architectures differ, so signal names are more portable than numbers. A sound escalation pattern sends SIGTERM, waits a few seconds, checks with pgrep, and escalates to SIGKILL only if processes remain.

Limitations, risks and common mistakes

pkill is powerful precisely because it acts on every match at once, and most incidents come from patterns that were broader than the operator assumed. The table below pairs typical symptoms with their causes and fixes.

SymptomCauseFix
SSH session drops unexpectedlypkill ssh also matched sshdUse -x or an anchored pattern
Script terminates itself-f pattern matches the script command lineUse a narrower pattern or pidfile
Nothing happens, exit code 1Name longer than 15 charactersShorten the pattern or add -f
Other users’ work is killedNo -u filter used as rootAlways add -u for user tasks
Corrupted data filesSIGKILL sent as first choiceStart with SIGTERM
Typical pkill mistakes and how to avoid them

pkill also has inherent race conditions: a process can exit and its PID can be reused between scanning /proc and calling kill(). Modern kernels offer pidfd-based signaling to close this gap, and services under systemd are better stopped with systemctl or systemctl kill, which use cgroups to reach every process in a unit, including those that changed their names.

  • Signaling a zombie process has no effect because it has already exited; its parent must reap it.
  • Processes in uninterruptible sleep (state D) do not react even to SIGKILL until the kernel operation finishes.
  • In shell scripts, quote the pattern so the shell does not expand special characters before pkill sees them.

In simple terms

Every running program on a Linux computer is a process, and each process can receive short messages called signals. Normally you need a process number to send one. pkill lets you describe the program by its name instead, and it finds and messages all matching programs for you.

Think of it as calling out a name in a crowded room rather than tapping one person on the shoulder. It is fast, but everyone with a similar name may answer. That is why careful administrators first ask who would answer, using pgrep, and only then send the message. Precise names keep pkill safe.

A practical example

A system administrator manages a Linux build server where nightly jobs start a Python worker with the command python3 /opt/build/worker.py --queue nightly. One morning, eight copies of the worker are still running and holding about 12 GB of memory, which slows the developers’ daytime builds.

Instead of searching for eight PIDs, the administrator runs pgrep -a -u builder -f 'worker.py --queue nightly' and confirms that exactly eight processes match, all owned by the builder account. She then runs pkill -e -u builder -f 'worker.py --queue nightly', which sends SIGTERM to all eight and prints each one. Five seconds later, pgrep shows two workers stuck in a network call, so she repeats the command with --signal KILL.

The result is that memory is freed in under a minute, no other user’s processes are touched, and the daytime queue, started with --queue day, keeps running because the pattern was specific enough to exclude it.