How to Kill Processes Using Linux

The killall command is a strong but customizable weapon against bad programs

Every so often a program hangs, in which case you need a method for killing it. Linux offers several approaches for force-terminating programs from the shell.

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 kills all three.

For example, open a small program such as an image viewer. Now open another copy of the same image viewer. This example uses Xviewer, which is a clone of Eye Of Gnome.

Open a terminal and type in the following command:

killall xviewer

Both instances of the program close.

Kill the Exact Process

The killall command may produce strange results. If you have a command name that is more than 15 characters long, the killall command only works on the first 15 characters. If, therefore, you have two programs that share the same first 15 characters, both programs are canceled even though you only wanted to kill one.

To get around this you can specify the following switch, which only kills 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 creates two processes:

ps -ef | less

One command is for the ps -ef part, which lists all 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, run the following command:

killall -g

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

killall -g bash

To list all the running groups run the following command:

ps -g

Get Confirmation Before Killing Programs

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

When you use the following switch, you are 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—this command kills processes running for four hours or longer.

Specify any one of the following time milestones:

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

Alternatively, to kill commands that have only just started running, use the following switch:

killall -y h4

This time the killall command kills 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 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, specify a regular expression so that all processes that match the regular expression are closed by the killall command.

To use a regular expression use the following command:

killall -r <expression>

Kill Programs for a Specific User

To kill a program owned by a specific user, use the -u switch:

killall -u <username> <programname>

Omit the program name to terminate all of that user's processes.

Wait for killall to Finish

By default, killall returns 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.

Run the following command:

killall -w

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

Was this page helpful?