Pkill is a Linux command-line utility that allows you to send signals to currently running processes. The most common signal is a kill signal, from whence comes its name. But killing a process is just one of the things you can do. You can also suspend and resume them, send user-defined signals, and even notify them. In this article, I’ll show you how to use pkill, when to use it, and how it’s superior to the more straightforward “kill” command.
How to Use pkill
We can use pkill to kill a process by specifying its name. Unlike with “kill”, we can’t use the process ID so you need to know its name.
So let’s say I use a sleep command and send it into the background using the “&” character like this:
sleep 300 &
The “&” character means that you can continue working on the terminal while the process executes in the background. You can use this symbol in conjunction with the nohup command to create a background process that won’t terminate even after you leave the command line.
After creating the command and sending it into the background, you can see that it runs with a certain process ID using the command:
ps
And here’s the output;
Here, the sleep command has process ID 25179. But since we can’t use the process ID for pkill, we can just use the command name instead. To kill the sleep command, we use:
pkill -9 sleep
The number “9” is the number assigned to the “SIGKILL” process. If you want, you can also use the following:
pkill -KILL sleep
Each signal is associated with both a number as well as a name. You can use the number if it’s faster, but I think the name is easier to remember.
This gives the following output:
As you can see, after I execute the sleep command, it says “Done” and the next time I run the “ps” command, it’s no longer there in the process list.
So that’s how you use a basic pkill command. There’s nothing complicated about it.
Pkill Pattern Matching
One of the best reasons to use pkill over kill is the former’s ability to use pattern matching to target processes. While the kill command strictly requires process IDs, it makes it hard to use consistently since process IDs aren’t the same every time you start the same process. By contrast, by focusing on the process name, administrators can share commands and scripts between themselves and ensure that the right processes will receive the signals they want.
Default Pattern Matching
By default, pkill’s “name” argument is a pattern that matches the entirety or just a substring. For example, the command:
pkill apache
Will affect all processes with the name “apache”. That means it will match both “apache” and “apache2”. This can be useful if you don’t know the version of apache that’s running and you want to shut it down regardless.
Naturally, this can also be dangerous. You want to make sure that the pattern you’re using is as tight as possible and can’t affect any other processes. Pkill has the potential to send signals to multiple processes at once, and if your pattern is too broad, it could have disastrous consequences like killing a whole bunch of processes that you didn’t intend to kill! You need to make sure, for example, that the pattern you’re using isn’t accidentally a substring of another, unrelated process.
Extended Regular Expressions
Pkill allows you to use regex to more accurately target processes. For example, consider the following command:
pkill '^httpd'
“httpd” is a process normally associated with the Apache web server. The “^” character indicates that the process name starts with httpd. This precaution ensures that processes containing “httpd” in the middle won’t be collateral damage.
Similarly, we can use the “$” character to indicate the end of a string as shown here:
pkill 'nginx$'
The above command will apply to all processes ending with “nginx”.
Case Sensitivity
By default, pkill pattern matches are case-sensitive. If, on the other hand, you want to tell pkill to ignore case, you can use the “-i” parameter like this:
pkill -i sshd
The above will match “SSHD” and “sshD” as well.
Matching the Exact Command Used to Start the Process
If the above techniques seem a bit broad to you, it’s understandable. After all, when considering which processes to kill, you might want to be as specific as possible. In the absence of process IDs, pkill still provides you with a highly specific way to target processes – namely the command that was used to start them.
To do this, we use the “-f” parameter like this:
pkill -f 'python my_script.py'
The above command will match the process that started with exactly the command “python my_script.py”. This kind of targeting will ensure that you don’t accidentally kill the wrong process.
Matching Processes Started by Users
Because Linux is a multi-user operating system, more than one user may run a command with the same command line. As regular users, pkill doesn’t allow us to kill processes started by other users, so you don’t need to worry about affecting what someone else has done. However, if you’re running as root, then you can see all the processes that everyone has started and kill them as well.
As root, to avoid killing the wrong person’s process, you should use the “-u” option to specify the user. For example:
pkill -u user1 sshd
The above command will target the process “sshd”, but only if it belongs to the “user1” user. Since SSHD is such a common process, such commands are necessary to ensure that you target the right users.
Previewing the Targets of Pkill
Since it’s so easy to kill processes with pkill, it’s a good idea to verify in advance that you’re targeting the right process. Doing so is easy with the “pgrep” command. For example, the following command:
pgrep -fl bash
Will show you the list of running processes that match your criteria. It won’t take any action on them, or kill them, but at least you can see what’s being targeted. The “-l” flag tells pgrep to show you the names of the processes along with the process IDs. Running this on my command line, I get this:
As you can see, I have two processes running, and the command shows me that it will target “bash” along with the process number. Without “-l”, I would just get the process number.
When to Use Quotes for Patterns
You can use pkill without quotes for the patterns for casual use cases. However, there are two scenarios where you must use quotes like this:
pkill -f 'python my_script.py'
First, if you have spaces in the pattern, you must use quotes. Second, if you’re using regular expressions, then quotes are necessary. It might be a good idea to get used to using quotes in general for pkill, given the stakes. Better safe than sorry!
Conclusion
Pkill is a great tool to easily kill stuff when you don’t want to bother with the process ID. This is useful when you want to create reusable code that works all the time. There are many ways to more carefully target which processes are affected, and you should use the “pgrep” tool to ensure that you’re not accidentally killing something that you shouldn’t.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply