pkill Command Examples in Linux

pkill command sends any specified signal, or by default the termination signal, to processes based on a matching pattern. Similar to the pgrep command, but actually sends a signal instead of printing to stdout. For example, if you start top in one terminal, and then issue pkill top in another terminal, you’ll see that top terminates. The command matched a name pattern rather than a process ID.

Syntax

The syntax of this command is:

# pkill [options] {pattern}

The kill and pkill commands send signals to processes directing them to terminate. Each signal has a number, name, and an associated event. Below are some of the most commonly used signals with their functionalities.

Number Name Description
1 SIGHUP Reload Configuration File
2 SIGINT Interrupt by keyboard (ctrl+c)
9 SIGKILL kill process
15 SIGTERM End process immediately. (Terminate a process in controlled manner so cleanup is possible)
18 SIGCONT Continue the process stopped with STOP
19 STOP Stop process
Note: However, there are processes that should not be terminated, such as the init process. Killing such processes can result in a system crash. A superuser can kill any process in the system.

pkill Command Examples

1. Kill all processes which match:

# pkill "process_name"

2. Kill all processes which match their full command instead of just the process name:

# pkill -f "command_name"

3. Force kill matching processes (can’t be blocked):

# pkill -9 "process_name"

4. Send SIGUSR1 signal to processes which match:

# pkill -USR1 "process_name"

5. Kill the main `firefox` process to close the browser:

# pkill --oldest "firefox"

6. The pkill command can be used to send a signal to processes running from a specific controlling Terminal, as shown in the following syntax:

# pkill -t [terminal_name] -U UID [command_pattern]
Related Post