Linux pkill Command Tutorial for Beginners (5 Examples)
In Linux, if you need to kill a process (for whatever reason) through the command line, you can use the kill command, which requires you to pass as input the ID of the process you're trying to terminate. But did you know that there also exists a way to kill processes without specifying their PIDs?
Yes, there's a tool - dubbed pkill - that lets you do this. In this article, we will discuss the basics of this command using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS and Debian 10 machine.
Linux pkill command
The pkill command in Linux is basically an easier way to kill processes. Following is its syntax:
pkill [options] pattern
And here's what the man page says about this tool:
pkill - signal processes based on name and other attributes
Following are some Q&A-styled examples that should give you an ever better idea on how the pkill command works.
Q1. How to use pkill command?
Basic usage is pretty straight straight forward - all you have to do is to execute 'pkill' with process name as input.
For example, if you're trying to kill the 'gedit' process, you can do that using pkill in the following way:
That's it. You'll see the gedit process (if running) will get killed upon execution of the above-mentioned command.
Q2. How to make pkill send a different signal?
As already mentioned in the beginning of this article, the pkill command basically sends a signal to the process. By default, it's the SIGTERM signal that gets sent, but if you want, you can change the signal using the --signal command-line option.
Q3. How to kill processes based on full command line?
By default, the input name (pattern) is matched against the process name. However, if you want, you can make pkill match the complete command line instead. This can be done using the -f command-line option.
For example, let's say there are two ping commands running on my system. Following is an excerpt taken from the ps command output:
root 19253 0.0 0.1 9180 2916 pts/1 S+ 19:24 0:00 ping google.com
root 19331 0.0 0.1 9180 2912 pts/2 S+ 19:25 0:00 ping howtoforge.com
Now, suppose the requirement is to only kill the 'ping google.com' command using pkill.
If you use pkill in the following way:
then both the commands will be killed. So to avoid that, we can use the -f command-line option. Here's how it can be used:
pkill -f "ping google.com"
I've executed the command in the first terminal window:
Result in the second terminal window where the ping process was running:
So when the aforementioned command was executed, only the command mentioned as input was killed.
Q4. How to make pkill case insensitive?
By default, the pkill command is case sensitive, meaning it treats names in upper case and lower case differently. However, if you want, you can force pkill to be case insensitive, something which you can do using the -i command-line option.
pkill -i [process-name]
Q5. How to make syslog reread its configuration file using pkill?
You can use the pkill command in the following way to achieve this:
pkill -HUP syslogd
So pkill is an extremely useful command when it comes to killing processes. We've already discussed some of the command line options this command provides. Once you're done practicing these, you can learn more about the command by heading to its man page.