Using the pkill command, Linux systems can end their running processes. One of its unique features is that it does not require entering the process’s PID number like the kill command. You can end that process by entering the process’s name as input. In most cases, we know which process we have to stop, and we know its name. It proves to be an easy command-line method for us to finish the complicated process. This article will describe the details of various methods using the pkill command and try to understand it completely.
pkill Command in Linux
Below is the standard syntax for the pkill command:
We specified the matching <PATTERN> using an extended regular expression.
If a process matches an alternative signal, the pkill command is invoked with the –signal option. An alternative way of sending signals is that a hyphen prefixes the number or signal name (-) after running pkill.
You can list all available prompts by using the kill -l command. We list more common signals, as shown below:
15 (TERM): | For stopping any process completely |
9 (KILL): | For killing a process |
1 (HUP): | For reloading a process |
There are three ways to specify signals:
- Without the “SIG” prefix, such as -HUP
- With the “SIG” prefix like -SIGHUP
- Using a number, such as – 1
Kill Process by Process Name
Invoked with no options, pkill starts the termination of all running programs by sending the 15 (TERM) signal. If you want to stop all Firefox processes gracefully, you can run the following command:
When one or more running process names match the request, the command returns 0. Otherwise, the exit code is 1. This may be useful when writing a shell script.
Note: At least 15 characters in the partial name of the process must appear.
Reload Process
You can reload any “X” process by running the following command.
Here, “X” is the process that you want to reload.
Kill the Process Starts and Ends With Specific Expression
You can use the pkill regular expression to match process names. Using the pgrep command is more beneficial if you print the matched processes first before sending a single one. For example, you can run the following command for listing out everything that starts with “ssh”:
A caret “*” should appear before the string’s beginning, and a dollar “$” should appear at its end.
Kill Process Based on Full Command
The pkill command matches only process names by default. Through the -f option, we can instruct pkill to execute the completed command instead of the process name.
Suppose there are two types of ping commands running in your system, and you will run the following command to stop them:
Both will kill the ping commands running in your system by using the previous command. To avoid this problem, the -f command-line option is used.
Here, “A” can be any one ping of your system.
The previous command will kill only the ping inputted when it runs.
Send a Different Signal to Kill a Process (-signal)
By default, the pkill command uses the “SIGTERM” signal during its execution. You can change the default signal by using the “–signal “option with the pkill command:
Insensitive pkill Case
The pkill command treats names in lower case and upper case differently because this command is case-sensitive. To make the pkill case insensitive, you can use the -i option. To do so, run the following command:
The -u option is used to specify pkill to match the processes being run by a specific user.
Here, “X” is the user’s name.
Separate multiple users with commas if you want to specify more than one.
“X”, “Y”, and “Z” are three different users.
You can also combine search patterns and options. You must run the following command to send KILL signals to all processes under the “X” user and “Y”.
“X” is the username, and “Y” is the search pattern.
The -o and -n options display the least recently oldest or most recently started processes.
For example, run the following command to kill the least recently oldest screen:
Conclusion
The pkill command signals a program to run based on various parameters. If you want to stop a process and know it by name, then you can use the pkill command to kill that process. In this article, we have seen many uses of the pkill command based on many practical examples. We hope that with the help of this article, you will adequately understand the pkill command and kill the process. Check the other Linux Hint articles for more tips and tutorials.