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:

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:

root 19253 0.0 0.1 9180 2916 pts/1 S+ 19:24 0:00 ping
root 19331 0.0 0.1 9180 2912 pts/2 S+ 19:25 0:00 ping

Now, suppose the requirement is to only kill the 'ping' 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"

I've executed the command in the first terminal window:

Process killed with pkill

Result in the second terminal window where the ping process was running:

Process in second terminal stopped with pkill command

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.

Share this page:

2 Comment(s)

Add comment

Please register in our forum first to comment.


By: Martin

This is exactly what I was looking for.I am running motion with several ipcameras running to collect their respective motion detections and snaphots images. Sometimes I need to make changes to the their respective ipcam's conf files and have to stop/restart the motion daemon.

I needed to get the pkill pattern matching right so that it only saw and stopped the motion app and not the script which had the name "motion" in it.

The script I wrote is the following:


set -xv#!/bin/bash#---------------------# if no motion running then result = "1"# ps -ef |grep -i [m]otion# echo $?# 1# ps -ef |grep -i [m]otion## if motion is running then result = "0"# ps -ef |grep -i [m]otion# root     20046     1  0 17:10 ?        00:00:00 sudo motion# root     20047 20046 27 17:10 ?        00:00:02 motion# echo $?# 0#---------------------datesudo pkill -f "sudo motion"sleep 5 # wait five second before testing to see if motion has been stoppedps -ef |grep -i [m]otion |grep -i "[s]udo motion"OUT=$?echo "before while:" $OUTwhile [ $OUT -ne 0 ]do    echo ""   echo "...waiting for motion to restart..."   echo ""   date   ps -ef |grep -i [m]otion |grep -i "[s]udo motion"   OUT=$?   echo "inside while:" $OUT   sleep 5 # delay 5 seconds before testing againdoneecho ""dateecho ""echo "motion has been restarted successfully..."echo ""


Thank you very much for posting your examples.


By: Vlado

I am facing the issue with pkill execution inside the bash script. It seems that pkill kills the whole process and next commands are not executed. Do you know what is going on there? Have a nice day.