4 Ways to Kill a Process – kill, killall, pkill, xkill

by Ramesh Natarajan on December 2, 2009

Kill UNIX / Linux Process Immediately

Photo Courtesy: seiho

Kill command is use to send signal to a process or to kill a process. We typically use kill -SIGNAL PID, where you know the PID of the process.

There are other ways to effectively kill a process — killing a process by name, killing a process by specifying part of the name, killing a process by pointing out the process with cursor etc.,

In this article, let us review 4 ways to kill a process.

1. Kill Command – Kill the process by specifying its PID

All the below kill conventions will send the TERM signal to the specified process. For the signals, either the signal name or signal number can be used. You need to lookup the pid for the process and give it as an argument to kill.

$ kill -TERM pid

$ kill -SIGTERM pid

$ kill -15 pid

Example: Kill the firefox process.

$ ps -ef | grep firefox
1986 ?        Sl     7:22 /usr/lib/firefox-3.5.3/firefox

$ kill -9 1986

2. Killall Command – Kill processes by name

Instead of specifying a process by its PID, you can specify the name of the process. If more than one process runs with that name, all of them will be killed.
Example: Kill all the firefox processes

$ killall -9 firefox

3. Pkill Command – Send signal to the process based on its name

You can send signal to any process by specifying the full name or partial name. So there is no need for you to find out the PID of the process to send the signal.

Example: Send SIGTERM to all the process which has sample in its name.

$ pkill sample

Pkill Example:

Before sending signal, you can verify which are all the process is matching the criteria using “pgrep -l”, which displays the process ID and process name of the matching processes.

In this example, all the processes are designed to log the signal to signal-log, along with its PID.

$ pgrep -l sample
12406 sample-server.p
12425 sample-server.p
12430 sample-garbagec

$ pkill -USR1 sample

$ cat signal-log
Name: ./sample-server.pl Pid: 12406 Signal Received: USR1
Name: ./sample-server.pl Pid: 12425 Signal Received: USR1
Name: ./sample-garbagecollector.pl Pid: 12430 Signal Received: USR1

Note: The part of name which you specify should be in the character within the first 15 character of the process name.

4. Xkill Command – kill a client by X resource

xkill is the simplest way to kill a malfunctioning program. When you want to kill a process, initiate xkill which will offer an cross-hair cursor. Click on the window with left cursor which will kill that process.

$ xkill
Select the window whose client you wish to kill with button 1....
xkill:  killing creator of resource 0x1200003

Note: Actually, xkill instructs XServer to terminate the client.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 20 comments… read them below or add one }

1 Yuri Baranov December 2, 2009 at 5:23 am

You may also mention pgrep utility as an useful companion of pkill.

In KDE Ctrl-Atl-Esc shortcut launches xkill.

I do not know, if a GNOME shortcut does exist.

2 Ramesh Raman December 2, 2009 at 11:05 pm

Dear sir,
I am very much interest to read your tips and examples. at the same time you can publish server configuration related topics and what are the things available in linux like windows toolls team viewer and remote system monitoring tolls. please it is very useful to me i dont know what are the tools available in linux and how to configure.

3 Edson December 7, 2009 at 11:21 am

Nice .. Killl process tips

4 Ramesh Natarajan December 10, 2009 at 5:46 pm

@Yuri,

Thanks for mentioning about pgrep. You are right. pgrep and pkill goes together. Actually when we do man pkill, we get information about pgrep also.

For those who don’t know about pgrep, following is the snippet from ‘man pgrep’:

       pgrep looks through the currently running processes and lists the  pro-
       cess  IDs which matches the selection criteria to stdout.  All the cri-
       teria have to match.  For example,

       pgrep -u root sshd

       will only list the processes called sshd AND owned  by  root. 

@Ramesh Raman,

I will write few articles about GUI based server configuration tools available for Linux. I’ve added it in my list of things to do. Thanks for the suggestion.

@Edson,

I’m very glad that you found this article helpful.

5 Gabriel Rodriguez December 20, 2009 at 6:57 am

wow i didnt know you could do that, thanks a lot!!

6 Louis Watson March 12, 2010 at 2:36 pm

Thank you for this – it was very helpful for a newbie like me !

7 James October 4, 2010 at 9:33 am

Not all of these things work like I would expect on SuSE 11.3. In particular, there seems to be issues killing things by name instead of PID, perhaps related to them being child processes. For example, I can kill perl itself (which also kills any perl programs running) but I am frequently unable to kill just the program process without killing the perl interpreter. Is there a reason for this? It would be a lot more convenient to be able to kill by name than PID, but it just doesn’t seem to work the same same. In general, it tells me the process wasn’t found, when I just grepped it and I know it’s there.

8 goutam January 7, 2011 at 11:07 pm

informative at its best !!!

9 Omari November 3, 2011 at 12:40 pm

When there are multiple processes using the same process name, you may want to kill one of the processes but not all. Doing a “pkill processname” will kill all processes with that name (no matter how many processes there are).

Here’s a bash script that I wrote that allows you specify which process of the processname’s you want to kill.

you run it like so: “script.sh processname filtername”

This would kill all processname’s that match filtername.

Here is script.sh:

#!/bin/bash
lookup=$1
finelookup=$2

PIDs=”$(pgrep -l $lookup | tr ‘ ‘ ‘=’ | awk -F= ‘{print $1}’)”

for i in ${PIDs} ; do
toKill=”$(ps -ef | grep $i | grep $finelookup)”
if [ "$toKill" != "" ]; then
kill -9 $i
fi
done

10 dev July 18, 2012 at 3:17 am

nice one bro….. very helpful….

11 dhanda vijay August 21, 2012 at 3:20 am

marevellous

12 iceberg February 27, 2013 at 2:54 pm

hi,
very nice tips in this place.
i have a strange phenomena:
i added to my crontab the following line
31 22 * * 0-4 /usr/bin/pkill -9 -f popeye
strangely cron doesn’t even tries to execute the command that works perfectly from the shell.
i’ll be glad to have ideas how to solve this.
10x

13 Spook March 5, 2013 at 7:22 am

Hi,
nice hints for the usage – especially about pkill which I am interested in.
I tried to include a pkill command in a debian boot script to kill a startup process which is still running after having fired up icecast2 an ezstream.

The comman I use in the boot script /etc/init.d/icecast2
is the following

pkill -fx “startpar -f — icecast2″

If i run this command from a terminal it works fine – but within the start script not.

Any ideas what could be the difference causing not to work this command line from within the start script ?

Tia
Michael

14 tom March 26, 2013 at 7:43 am

hi im new to linux, could you help me please?
im trying to get ids on processes which has 7 or larger oom_adj priority, and kill them.
can you give me some examples on how to do it?
thanks in advance:)

15 lms April 17, 2013 at 5:21 pm

thanks.

16 Amit July 2, 2013 at 3:18 am

Hey this script is vaery good seems it will beneficail for me …but it give the following respons

kill: ””: arguments must be process or job IDs

i m working on the Mac Os….. and using an app “Appium” and want to kill the server process run by that app not the GUI of the appium …

17 Ramlal July 12, 2013 at 5:39 am

Can any one tell me:
What is the difference between kill -9 and kill -3 commands?
why we use different numbers to kill the process?

18 Chintan Umarani August 2, 2013 at 4:30 am

By default kill executes Kill 15 that means kill process gently.
When you suggest to kill 9 it means killing process abruptly.

I am not sure about kill 3.

19 Rizwan October 20, 2013 at 3:02 pm

Thanks Much !!Ramesh Natarajan
It is very useful.

@Ramlal give this command ”kill -l” you will get to know the numbers details and their singal names :)

20 Ajay February 21, 2014 at 4:34 am

Ramesh………i have one doubt.

i heard that “kill -2″ is good to use compared to kill -9. wha t is the difference between two commands?

Leave a Comment

Previous post:

Next post: