In the Linux systems, the pkill command is proven to be useful when you have to terminate single or multiple processes in the Linux System. This article covers the basics of pkill command and it’s options as well. pkill command can kill given running program instantly on the Linux based systems. You can specify the processes which are running either with full or partial names or using other alternative attributes.
The command pkill itself is a part of the procps package in Linux. Usually, it is the pre-installed on your system. If it’s not installed, you can install it from the Linux repository. pkill command is a wrapper command being used with a pgrep program which displays the list of processes.
How to use pkill command
The syntax of the pkill command is below.
pkill [OPTIONS] <PATTERN>
In the above command, you have to provide [OPTIONS] and the <PATTERN> with regular expressions. The command will follow the regular expressions and act accordingly.
When running pkill without any option specified, it will send 15 (TERM) signal to the process IDs (PIDs) of all running programs that match the given name. Let’s assume you want to stop all firefox processes, then you have to run the following command.
pkill -9 firefox
If the output returns 0, it means it has at least one process available with the requested name “firefox”. If no results it will return exit code 1. These codes are very useful when used in shell scripts.
To send a different signal using the pkill command, you can use the pkill command with the option –signal with a numeric or symbolic name. Another method is to use pkill with the signal name or a number with prefix (-) hyphen.
To check all available signals, we can use the pkill -l command. Have a look, at the most commonly used signals below.
- 1 (HUP): It is used to reload a process.
- 9 (KILL): To kill the process.
- 15 (TERM): To stop the process.
You can specify the signals with three different ways:
- Use a number with a prefix. (e.g; -1)
- Using the “SIG” prefix. (e.g; -SIGHUP)
- Without using the “SIG” prefix. (e.g; -HUP)
For example, If you want to reload Nginx processes, you can run the command as below.
pkill -HUP nginx
The command pkill takes regular expressions as an argument in the command. You also need to specify the process names that you want to kill. It is advisable to use the pgrep command to display the matched processes first. After checking the processes, you can send signals to those processes.
Let’s have an example of the same. If you want to send signals to the processes which contain “ssh” in their names use the command as below.
If you want to match processes with the full argument, you can use -f option as below.
pkill -9 -f "ping 188.8.131.52"
Another option -u helps you to find out and kill the processes of specified users.
pkill -u localuser
In the above command, replace the word “localuser” with your system username. Alternatively, you can specify multiple usernames separated by commas.
pkill -u localuser, gnome, Linux
Conclusion on pkill command
As we already learned, the pkill command is useful to pass on the signals to find out the running programs in Linux. Using different pkill regular expressions, you can kill processes instantly. pkill also allows to find out processes by username. Additionally, you can kill/stop the selected process in Linux using the pkill command.