| title | Linux Command Tutorial: pkill | ||||
|---|---|---|---|---|---|
| date | 2026-09-12 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-pkill-tutorial | ||||
| description | Authoritative reference tutorial for pkill (procps-ng), detailing signal dispatch by process pattern, user targeting (-u), exact name matching (-x), and safety boundaries. | ||||
| upstream_suite | procps-ng | ||||
| upstream_version | procps-ng 4.0.4 | ||||
| posix_standard | None | ||||
| research_date | 2026-09-12 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream: procps-ng 4.0.4 | POSIX: De-facto Standard (Not POSIX standardized) | Safety Tier: privileged-system-destructive | Scope: process-signaling
pkill sends specified signals (by default SIGTERM) to processes matching selection criteria. It combines process discovery with signal delivery, eliminating the need to look up PIDs manually before invoking kill.
- Upstream Project & Provenance: Maintained within procps-ng (
procps-ng). - Portability & Standards Baseline:
pkillis a de facto standard across UNIX and Linux systems; not defined in POSIX.1-2024. - Target Research Implementation: Audited against procps-ng 4.0.4 (
pkill(1)). - Applicability & Lifecycle: The standard command for terminating processes by name, killing user sessions, or broadcasting configuration reloads (
SIGHUP).
pkill [options] patternpkilllocates all processes matchingpatternand invokes thekill(2)system call on each matched PID.- Default Signal: If no signal is explicitly specified,
pkilltransmitsSIGTERM(Signal 15), requesting graceful process shutdown. - Signals can be specified numerically (
-9) or symbolically (-KILL,--signal SIGTERM).
| Flag | Description | Default | Upstream Note |
|---|---|---|---|
-SIGNAL |
Signal to send (e.g. -9, -HUP, -INT). |
SIGTERM (15) |
Graceful termination |
-x |
Exact match: require pattern to match the entire name. | Substring | Critical safety flag |
-f |
Match pattern against full command line arguments. | Basename | Required for scripts |
-u user |
Target only processes owned by effective user ID or name. | All users | User scoping |
-U user |
Target only processes owned by real user ID or name. | All users | User scoping |
-e |
Display what process was killed (echo mode). | Silent | Auditability |
-c |
Suppress normal signal dispatch; display count of matched processes. | Signal dispatch | Dry-run safety |
| Operation | Command | Notes |
|---|---|---|
| Graceful terminate by exact name | pkill -x nginx |
Sends SIGTERM (15) to exact binary match |
| Force kill unresponsive process | pkill -9 -x frozen_app |
Sends uncatchable SIGKILL (9) |
| Reload daemon configuration | pkill -HUP -x sshd |
Sends SIGHUP (1) signal |
| Signal matching full arguments | pkill -f "python worker.py" |
Matches against full command line |
| Kill all processes of user | pkill -u deploy |
Targets processes belonging to user |
| Terminate with echo logging | pkill -e -x redis-server |
Outputs names and PIDs of signaled tasks |
| Count matching targets without signaling | pkill -c -x worker |
Dry-run count of potential targets |
pkill -x nginx- Sends
SIGTERMto all processes named exactlynginx.
pkill -HUP -x rsyslogdVisualizing which processes were signaled:
pkill -e -x php-fpmphp-fpm killed (pid 4512)
php-fpm killed (pid 4513)
php-fpm killed (pid 4514)
If an application is unresponsive to SIGTERM:
pkill -9 -x worker_process- Technical Analysis:
SIGKILL(9) is handled directly by the kernel and cannot be caught, ignored, or blocked by the target process.
Logging off a rogue or terminated user completely:
sudo pkill -u baduser- Transmits
SIGTERMto every process owned bybaduser.
Caution
Catastrophic Substring Collisions: Running pkill sh without -x matches sshd, bash, ssh-agent, and any executable containing "sh", instantly severing remote administrative access. Always supply -x or verify candidates with pgrep first.
Because pkill without -x matches substrings, accidentally killing critical services is a serious risk.
The Safe Two-Stage Pattern:
- Run
pgrepwith identical flags first to inspect matching processes:pgrep -la "worker.py" - Once the target list is verified, execute
pkillwith the identical pattern:pkill -f "worker.py"
| Exit Code | Meaning |
|---|---|
0 |
One or more processes were matched and signaled. |
1 |
No processes were matched. |
2 |
Syntax error in options. |
3 |
Fatal error occurred. |
- An unprivileged user can only send signals to processes they own.
- Sending signals to processes owned by other users or system daemons requires root (
CAP_KILL).
Note
Processes trapped in the kernel D state (uninterruptible sleep waiting on hardware disk/NFS I/O) cannot be terminated even by pkill -9. The process only exits after the underlying I/O system call completes or times out.
- Always Use
-xfor Binary Names:- Guidance: Write
pkill -x <binary>instead ofpkill <binary>. - Authoritative Justification: Prevents catastrophic substring matches (e.g.
pkill shterminatingsshd).
- Guidance: Write
- Never Default to
SIGKILL(-9):- Guidance: Always attempt
SIGTERM(default) first before resorting toSIGKILL. - Authoritative Justification:
SIGKILLprevents applications from flushing database buffers, closing network sockets, or unlinking lock files.
- Guidance: Always attempt
- Verify with
pgrepBefore Executingpkill:- Guidance: Run
pgrep -abefore runningpkill -f. - Authoritative Justification: Discloses the exact process candidates before signal transmission.
- Guidance: Run
- procps-ng pkill(1) Manual: https://man7.org/linux/man-pages/man1/pkill.1.html
- Linux signal(7) Overview: https://man7.org/linux/man-pages/man7/signal.7.html