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 you 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 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 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:

pkill gedit

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.

For example:

pkill --signal SIGKILL gedit

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:

...
himanshu  4304  4296  0 10:03 pts/3    00:00:00 ping google.com
himanshu  4315  4306  0 10:03 pts/4    00:00: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:

pkill ping

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"

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

Conclusion

So pkill is an extremely useful command when it comes to killing processes. We've already discussed some of the command line option this command provides. Once you're done practicing these, you can learn more about the command by heading to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment