pkill
Kill processes by name
TLDR
Kill all processes which match
Kill all processes which match their full command instead of just the process name
Force kill matching processes (can't be blocked)
Send SIGUSR1 signal to processes which match
Kill the main firefox process to close the browser
SYNOPSIS
pkill [options] pattern
pkill [-signal] [options] pattern
PARAMETERS
pattern
An extended regular expression that is matched against the process names or full command lines (if -f is used). This is the primary way to identify processes.
-signal, --signal signal
Specifies the signal to send to the matching processes. signal can be a numeric value (e.g., 9 for SIGKILL) or a symbolic name (e.g., KILL, TERM, HUP). The default is SIGTERM.
-c, --count
Suppresses normal output; instead, print the number of matching processes. When no processes match, the exit code is 1.
-e, --exact
Select only processes whose name or command line exactly matches the pattern.
-f, --full
The pattern is matched against the full command line, not just the process name.
-g, --group group
Select processes belonging to the specified effective group(s) (IDs or names). Multiple groups can be specified with a comma-separated list.
-G, --real-group group
Select processes belonging to the specified real group(s) (IDs or names). Multiple groups can be specified with a comma-separated list.
-l, --list-name
Lists the process name of all matching processes instead of killing them. Useful for testing a pattern before execution.
-n, --newest
Select only the newest (most recently started) of the matching processes.
-o, --oldest
Select only the oldest (least recently started) of the matching processes.
-P, --parent ppid
Select processes whose parent process ID is one of the specified ppids. Multiple parent PIDs can be specified with a comma-separated list.
-t, --terminal term
Select processes attached to the specified terminal(s) (tty, pts/0, etc.). Multiple terminals can be specified with a comma-separated list.
-u, --euid user
Select processes whose effective user ID is one of the specified user(s) (IDs or names). Multiple users can be specified with a comma-separated list.
-U, --ruid user
Select processes whose real user ID is one of the specified user(s) (IDs or names). Multiple users can be specified with a comma-separated list.
-v, --inverse
Invert the match, i.e., select all processes EXCEPT those that match the criteria.
-x, --exact
This option is often an alias for -e, indicating an exact match against the process name or command line.
DESCRIPTION
pkill is a command-line utility used to send signals to processes based on their name or other attributes. It is part of the procps-ng suite of tools, which also includes pgrep. While kill requires a Process ID (PID) and killall typically kills all processes matching a name, pkill offers more granular control by allowing users to select processes based on various criteria such as user, group, parent process, terminal, or even the full command line. The default signal sent is SIGTERM (signal 15), which gracefully requests a process to terminate. However, any signal can be specified by name or number. This flexibility makes pkill a powerful tool for system administrators and users alike for managing running processes.
CAVEATS
Using pkill without careful consideration of the pattern and options can lead to unintended termination of critical processes. Always verify the processes that would be affected by first using pgrep with the same options, or by using pkill -l to list the names of matching processes before sending a signal. The -f (full command line) option can be powerful but also prone to matching too broadly if the command line arguments vary significantly.
REGULAR EXPRESSIONS
The pattern argument supports extended regular expressions (ERE). This allows for complex matching rules, such as ^httpd$
to match only processes named "httpd" and not "httpd-worker", or (firefox|chrome)
to match either Firefox or Chrome processes. Understanding ERE syntax is crucial for effective use of pkill.
SIGNALS
Signals are a form of inter-process communication used by the operating system to notify a process of an event or to request it to perform a specific action, such as terminating. Common signals include SIGTERM (15, graceful termination), SIGKILL (9, immediate termination, cannot be caught or ignored), and SIGHUP (1, typically used to reload configuration files).
HISTORY
pkill was developed as part of the procps-ng utility suite, which provides essential system monitoring and management tools. It emerged as a more flexible and robust alternative to older commands like killall for situations requiring process selection based on criteria beyond just the process name. Its development was closely tied to that of pgrep, sharing much of its underlying logic for process identification.