How To Kill Processes Using Linux

Infinite Loop
Infinite Loop.

Introduction

Most of the time you will want a program to end by its own means or if it is a graphical application by using the appropriate menu option or by using the cross in the corner.

Every so often a program will hang in which case you will need a method for killing it. You might also want to kill a program that is running in the background that you no longer need to run.

This guide provides a method for killing all versions of the same application that are running on your system.

How To Use The killall Command

The killall command kills all the processes by name. That means if you have three versions of the same program running the killall command will kill all three.

For example, open a small program such an image viewer. Now open another copy of the same image viewer. For my example I have chosen Xviewer which is a clone of Eye Of Gnome.

Now open a terminal and type in the following command:

killall

For example to kill all instances of Xviewer type the following:

killall xviewer

Both instances of the program you have chosen to kill will now close.

Kill The Exact Process
 

Earlier I mentioned that killall may produce strange results. Well here is one reason why. If you have a command name which is more than 15 characters long then the killall command will only work on the first 15 characters. If therefore you have 2 programs which share the same first 15 characters both programs will be cancelled even though you only wanted to kill one.

To get around this you can specify the following switch which will only kill files matching the exact name.

killall -e

Ignore Case When Killing Programs

To make sure the killall command ignores the case of the program name that you provide use the following command:

killall -I

killall --ignore-case

Kill All Programs In The Same Group

When you run a command such as the following one it will create two processes:

ps -ef | less

One command is for the ps -ef part which lists all of the running processes on your system and the output is piped to the less command.

Both programs belong to the same group which is bash.

To kill both programs at once you can run the following command:

killall -g

For example to kill all commands running in a bash shell run the following:

killall -g bash

 

Incidentally to list all the running groups run the following command:

ps -g

Get Confirmation Before Killing Programs

Obviously the killall command is quite a powerful command and you don't want to accidentally kill the wrong processes.

Using the following switch you will be asked whether you are sure before each process is killed.

killall -i

Kill Processes That Have Been Running For A Certain Amount Of Time

Imagine you have been running a program and it is taking a lot longer than you hoped it would.

You can kill the command in the following way:

killall -o h4

The h in the above command stands for hours.

You can also specify any one of the following:

  • s - seconds
  • m - minutes
  • h - hours
  • d - days
  • w - weeks
  • M - months
  • y - years

Alternatively if you wish to kill commands that have only just started running you can use the following switch:

killall -y h4

This time the killall command will kill all programs running for less than 4 hours.

Don't Tell Me When A Process Isn't Killed

By default if you try and kill a program that isn't running you will receive the following error:

programname: no process found

If you don't want to be told if the process wasn't found use the following command:

killall -q

Using Regular Expressions

Instead of specifying the name of a program or command you can specify a regular expression so that all processes which match the regular expression are closed by the killall command.

To use a regular expression use the following command:

killall -r

Kill Programs For A Specify User

If you want to kill a program that is being run by a specific user you can specify the following command:

killall -u

If you want to kill all of the processes for a particular user you can omit the program name.

Wait For killall To Finish

By default killall will return straight back to the terminal when you run it but you can force killall to wait until all the processes specified have been closed before returning you to the terminal window.

To do this run the following command:

killall -w

If the program never dies then killall will also continue to live on.

Signals Signals Signals

By default the killall command sends the SIGTERM signal to programs to get them to close and this is the cleanest method for killing programs.

There are however other signals you can send using the killall command and you can list them using the following command:

killall -l

The list returned will be something like this:

  • HUP
  • INT
  • QUIT
  • ILL
  • TRAP
  • ABRT
  • IOT
  • BUS
  • FPE
  • KILL
  • USR1
  • SEGV
  • USR2
  • PIPE
  • ALRM
  • TERM
  • STKFLT
  • CHLD
  • CONT
  • STOP
  • TSTP
  • TTIN
  • TTOU
  • URG
  • XCPU
  • XFSZ
  • VTALRM
  • PROG
  • WYNCH
  • IO
  • PWR
  • SYS
  • UNUSED

That list is extremely long. To read about what these signals mean run the following command:

man 7 signal

Generally you should use the default SIGTERM option but if the program refuses to die you can use SIGKILL which forces the program to close albeit in an undignified way.

Other Ways To Kill A Program

There are 5 other ways to kill a Linux application as highlighted in the linked guide.

However to save you the effort of clicking the link here is a section showing what those commands are are why you might use those commands over killall.

The first one is the kill command. The killall command as you have seen is great at killing all the versions of the same program. The kill command is designed to kill one process at a time and is therefore more targetted.

To run the kill command you need to know the process ID of the process you wish to kill. For this you can use the ps command.

For example to find a running version of Firefox you can run the following command:

ps -ef | grep firefox

You will see a line of data with the command /usr/lib/firefox/firefox at the end. At the beginning of the line you will see your user ID and the number after the user ID is the process ID.

Using the process ID you can kill Firefox by running the following command:

kill -9 <processid>

Another way to kill a program is by using the xkill command. This is generally used to kill misbehaving graphical applications.

To kill a program such as Firefox open a terminal and run the following command:

xkill

The cursor will now change to a large white cross. Hover the cursor over the window you wish to kill and click with the left mouse button. The program will exit immediately.

Another way to kill a process is by using the Linux top command. The top command lists all the running processes on your system.

All you have to do to kill a process is press the "k" key and enter the process ID of the application you wish to kill.

Earlier in this section I introduced you to the kill command and it required you finding the process using the ps command and then kill the process using the kill command.

This is not the simplest option by any means.

For one thing the ps command returns loads of information you don't need. All you wanted was the process ID. You can get the process ID more simply by running the following command:

pgrep firefox

The result of the above command is simply the process ID of Firefox. You can now run the kill command as follows:

kill <processid>

(Replace <processid> with the actual process ID returned by pgrep).

It is actually easier however to simply supply the program name to pkill as follows:

pkill firefox

Finally you can use a graphical tool such as the one supplied with Ubuntu called "System Monitor". To run "System Monitor" press the super key (Windows key on most computers) and type "sysmon" into the search bar. When the system monitor icon appears click on it.

The system monitor shows a list of processes. To end a program in a clean way select it and press the end key at the bottom of the screen (or press CTRL and E). If this fails to work either right click and choose "Kill" or press CTRL and K on the process you wish to kill.

 

Was this page helpful?