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.
Comments on this entry are closed.
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.
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.
Nice .. Killl process tips
@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’:
@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.
wow i didnt know you could do that, thanks a lot!!
Thank you for this – it was very helpful for a newbie like me !
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.
informative at its best !!!
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
nice one bro….. very helpful….
marevellous
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
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
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:)
thanks.
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 …
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?
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.
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 🙂
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?
it’s really in easy language
Which command is used to kill 1000 instance of a single process not using process name but using pid.
Hi,
I have a case that the killall does not work. On the OpenSUSE Tumbleweed linux, our app name is called fileflex.connector, and it’s installed at the /opt/fileflex, so when the app is running, if I run the command:
sudo killall fileflex.connector
the console will display: “no process found”
but if I run sudo killall /opt/fileflex/fileflex.connector
the process can be killed. That’s so weird.