pkill

process managementLinux/Unix
The pkill command is one of the most frequently used commands in Linux/Unix-like operating systems. pkill The pkill command sends signals to processes based on their name and other attributes. It's a more flexible alternative to killall, allowing pattern matching and filtering by various process attributes like terminal, parent PID, user, and more. This makes it a powerful tool for targeted process management.

Quick Reference

Command Name:

pkill

Category:

process management

Platform:

Linux/Unix

Basic Usage:

pkill [options] [arguments]

Common Use Cases

    Syntax

    pkill [options] pattern

    Options

    Option Description
    -signal, --signal signal Define the signal to send (default: TERM)
    -e, --echo Display what is killed
    -c, --count Count of matching processes (instead of signaling)
    -f, --full Match against full command line
    -g, --pgroup pgrp Match listed process group IDs
    -G, --group gid Match real group IDs
    -i, --ignore-case Case insensitive matching
    -n, --newest Select most recently started process
    -o, --oldest Select least recently started process
    -P, --parent ppid Match only child processes of the given parent PID
    -s, --session sid Match session IDs
    -t, --terminal term Match by controlling terminal
    -u, --euid euid Match by effective user ID
    -U, --uid uid Match by real user ID
    -v, --inverse Invert the matching (select processes NOT matching the criteria)
    -x, --exact Match exactly with the command name

    Examples

    How to Use These Examples

    The examples below show common ways to use the pkill command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Usage:

    pkill firefox

    Send the default TERM signal to all processes with "firefox" in their name.

    pkill -9 chrome

    Send the KILL signal (9) to forcefully terminate all processes with "chrome" in their name.

    pkill -HUP nginx

    Send the HUP signal to all processes with "nginx" in their name to reload configurations.

    Pattern Matching:

    pkill -f "python script.py"

    Match against the full command line, not just the process name.

    pkill -x firefox

    Match exactly "firefox", not processes containing "firefox".

    Process Filtering:

    pkill -u username firefox

    Kill only firefox processes owned by the specified user.

    pkill -t pts/0 bash

    Kill bash processes connected to the terminal pts/0.

    pkill -P 1234

    Kill all child processes of the process with PID 1234.

    Advanced Options:

    pkill -n firefox

    Kill only the newest (most recently started) process matching "firefox".

    pkill -o firefox

    Kill only the oldest (least recently started) process matching "firefox".

    pkill -v firefox

    Invert the match: kill all processes EXCEPT those matching "firefox".

    Combined Examples:

    pkill -9 -u username -x firefox

    Send KILL signal to processes exactly named "firefox" and owned by the specified user.

    pkill -HUP -f "nginx: master"

    Send HUP signal only to Nginx master processes by matching the full command line.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Common Signals:

    • 1 (HUP): Hangup - Often used to reload configuration
    • 2 (INT): Interrupt - Same as pressing Ctrl+C
    • 9 (KILL): Kill - Forceful termination, cannot be caught or ignored
    • 15 (TERM): Terminate - Default signal, graceful termination
    • 18 (CONT): Continue - Resume a stopped process
    • 19 (STOP): Stop - Pause process, cannot be caught or ignored

    Relationship with pgrep:

    • pkill and pgrep share the same command-line options and selection criteria
    • pgrep outputs matching PIDs, while pkill sends signals to matching processes
    • pkill is essentially pgrep with a signal-sending action
    • The man page for pkill is often combined with pgrep

    Process Selection:

    • By default, pkill matches against the process name (as displayed by ps -o comm=)
    • Use -f to match against the full command line (as displayed by ps -o args=)
    • Use -x for exact matches instead of partial string matching
    • Use -i for case-insensitive matching

    Process Filtering:

    • -u/--euid filters by effective user ID (numeric ID or username)
    • -U/--uid filters by real user ID
    • -g/--pgroup filters by process group
    • -G/--group filters by real group ID
    • -P/--parent filters by parent process ID
    • -s/--session filters by session ID
    • -t/--terminal filters by controlling terminal

    Process Selection Order:

    • -n/--newest selects only the most recently started matching process
    • -o/--oldest selects only the least recently started matching process
    • Without -n or -o, pkill affects all matching processes

    Pattern Matching:

    • The pattern is treated as an extended regular expression by default
    • Simple patterns like "firefox" will match any process with "firefox" in its name
    • More complex patterns like "^firefox$" can be used for precise matching
    • Use the -v option to invert the match (affect processes NOT matching the pattern)

    Exit Status:

    • 0: One or more processes matched the criteria
    • 1: No processes matched the criteria
    • Other values: Error occurred

    Permissions:

    • Users can only send signals to their own processes unless they have root privileges
    • Use sudo to signal processes owned by other users
    • Even with root privileges, some kernel processes may be protected

    Common Use Cases:

    • Terminating all processes related to a specific application
    • Reloading configuration for all processes of a certain type
    • Targeting processes based on complex criteria
    • Cleanly shutting down all processes associated with a specific user
    • Selectively terminating processes by their attributes

    Advantages over kill and killall:

    • More flexible matching criteria than both kill and killall
    • Can filter processes by user, terminal, parent PID, and more
    • Supports pattern matching with regular expressions
    • Can match against the full command line, not just process name
    • Options to select only newest or oldest matching processes

    Safety Considerations:

    • Use the -c (count) option first to check how many processes would be affected
    • Use the -e (echo) option to see which processes will be signaled
    • Always use the least destructive signal first (TERM before KILL)
    • Be specific with patterns to avoid unintended matches
    • Consider using -x for exact matches when targeting specific programs

    Examples of Signals:

    • pkill -1 (HUP) httpd: Reload Apache configuration
    • pkill -15 (TERM) firefox: Gracefully terminate Firefox
    • pkill -9 (KILL) stuck_process: Force kill a process that won't terminate
    • pkill -19 (STOP) resource_heavy_app: Pause a resource-intensive application
    • pkill -18 (CONT) paused_app: Resume a previously paused application

    Related Commands:

    • pgrep - List processes based on name and other attributes
    • kill - Send signals to processes by PID
    • killall - Kill processes by exact name
    • ps - Display information about active processes
    • top - Display and manage process activity
    • htop - Interactive process viewer
    • pidof - Find the process ID of a running program

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the pkill command works in different scenarios.

    $ pkill
    View All Commands