pkill Command

The pkill command in Linux is a command-line utility used to send signals (like termination signals) to processes based on their name or other attributes. It is a more powerful and flexible alternative to killall and is often used in scripts for efficient process management.

Syntax

pkill [OPTION]... PATTERN

Description

pkill works by searching for processes that match the provided PATTERN (which is a regular expression by default) and then sending a specified signal to them. If no signal is specified, it sends SIGTERM (signal 15), which is a request for graceful termination. It is part of the procps-ng package, along with pgrep.

Common uses include:

  • Terminating processes by name or partial name.
  • Sending specific signals to processes (e.g., SIGHUP to reload configurations).
  • Killing processes owned by a particular user or group.
  • Targeting processes based on their parent process ID.

Common Options

Option Description
-SIGNAL, -s SIGNAL Specify the signal to send (e.g., -9 for SIGKILL, -HUP for SIGHUP).
-u, --euid <user> Match processes whose effective user ID is <user>.
-G, --egid <group> Match processes whose effective group ID is <group>.
-f, --full The pattern is matched against the full command line.
-n, --newest Select the newest (most recently started) of the matching processes.
-o, --oldest Select the oldest (least recently started) of the matching processes.
-x, --exact Only match processes whose name is exactly PATTERN.
-P, --parent <ppid> Match only children of the process with specified parent PID.

Examples

Terminate a process by name

pkill firefox

Sends a SIGTERM signal to all running processes named firefox.

Forcefully kill a process

pkill -9 chrome
# Or:
pkill -s SIGKILL chrome

Sends a SIGKILL signal (signal 9) to all chrome processes, which cannot be ignored.

Send SIGHUP to a process (e.g., for graceful reload)

pkill -HUP nginx

Sends a SIGHUP signal to all nginx processes, often used to tell a daemon to reload its configuration files.

Kill processes owned by a specific user

pkill -u guest_user

Kills all processes owned by the user guest_user.

Kill processes matching a full command line

pkill -f "java -jar my_application.jar"

Kills processes whose full command line matches the specified pattern.

See also