The pkill command in Linux is useful for terminating processes based on their name or other attributes. When executed, pkill sends a signal to the specified processes, causing them to terminate.
It is important when a process becomes unresponsive and affects the system performance; the pkill command kills that process to eliminate the system lag. Moreover, the Administrators can create scripts that use pkill to automate the termination of multiple processes with a single command.
In short, the pkill is an essential command line utility for every Linux-based system. This post will provide the basic and advanced usage of the pkill command in Linux.
Note: The practical demonstration of the commands in this post is carried out on Ubuntu 22.04.
How to Use the pkill Command in Linux?
The core working of each Linux command depends on the syntax followed by the command. So, before getting into its details, let’s look at the syntax of the pkill command:
$ pkill [options] pattern
The pkill command takes a pattern argument: a process name, process ID (PID), or other attributes such as username or signal. When you execute the pkill command with a specific pattern, it sends a signal to all the matching processes, causing them to terminate.
Here are some commonly used options with the pkill command:
- -u: specifies the effective user ID of the process to match.
- -x: matches only processes with the same name as the pattern.
- -f: matches against the full command line, including arguments.
- -signal: specifies the signal to send to the processes (default is SIGTERM).
You can get the detailed working and functionality of the pkill command using the command:
$ pkill --help
If you observe one of the options in the above output is the “-<sig>” or “–signal.” These refer to a specific signal that is sent with the pkill command. There are various signals used, which are described as follows:
- SIGTERM (15): This signal is sent to gracefully terminate a process. It allows the process to perform cleanup operations before exiting. This is the default signal sent by pkill.
- SIGKILL (9): Sent to force a process to immediately terminate without any cleanup operations. This is a harsh way to terminate a process and should be used as a last resort.
- SIGHUP (1): Used to notify a process that its controlling terminal has been closed. It is commonly used to restart daemons or reload their configuration files.
- SIGSTOP (19): Used to suspend a process. The process is not terminated, but it is stopped from executing until it receives a SIGCONT signal.
- SIGCONT (18): Resumes a suspended process. It is sent after a process has been suspended with the SIGSTOP signal.
- SIGUSR1 (10) and SIGUSR2 (12): User-defined signals that can be used for any purpose.
Examples of the pkill Command
Based on the syntax and the options of the pkill command, we will list the possible use cases of the pkill command. Let’s dig into these, one by one:
Example 1: Terminate All Processes
The primary purpose of the pkill command is to terminate all the processes identified by a specific property. It is useful when a process becomes unresponsive or hangs and needs to be terminated forcefully. For instance, the command written below terminates all processes with the name “firefox.”
$ pkill firefox
Example 2: Terminate a Process Using Process ID
Like the process name, the PID of a process can be used with the pkill command will terminate that process instantly. The command provided below terminates the process having process ID 2837.
$ pkill -P 2837
Note: You can use the “ps -ef” command to get the process id of any running processes that you want to terminate.
$ ps -ef
The second column of the output indicates the process IDs of the running processes.
Example 3: Terminate All Processes of a Specific User
One of the more distinctive uses of the pkill command is to terminate all the processes of a specific user. For this, the “-u” flag of the pkill command is used alongside the username. The command provided below will terminate all the associated processes with the user name “vboxuser”:
$ pkill -u vboxuser
Note: Try to use this command carefully. Suppose you use this command to terminate all processes of the logged-in user. Then, you may face an instant log out, and all the running processes will be terminated.
Example 4: Terminate Processes With a Specific Signal
The signal sent by pkill can terminate, suspend, or resume the processes. The SIGKILL signal is a forceful termination signal that terminates the process immediately without allowing it to perform any cleanup operations. The following command terminates all processes named “firefox” using the SIGKILL signal.
$ pkill -9 firefox
Example 5: Terminate Processes With Exact Name
The pkill command with its “-x” options terminates the processes that match the exact phrase used after the option. The “-x” flag is quite handy when you want to target a specific process (not all that contains the name).
The command provided below will terminate all the processes named “firefox”:
$ pkill -x firefox
Example 6: Terminate Process With Specific Command Name Arguments
The “-f” option of the pkill command is used to terminate the processes that match a command argument of a process. It is useful when you want to terminate a specific process with a specific set of arguments. For instance, the command below is “firefox –private-window,” will terminate the private window of the
$ pkill -f "firefox --private-window"
The pkill is one the essential utility to be used regularly by the system/network administrators. This command kills all the specific processes that match a name or the process id. Moreover, the pkill command offers numerous options to kill the processes in a customized manner.
All the basic understanding and advanced-level usage of the pkill command has been illustrated with the help of the syntax and best-suited examples. For more tutorials on Linux-Commands, visit us at Linux-Genie.