If you’re like most Linux users, you probably have a few processes running in the background that you don’t need or want. Maybe your email client is keeping track of new messages, or your web browser is refreshing every few seconds. But what if you wanted to kill all of those processes? It’s not as hard as you might think. In this article, we’ll show you how to do it on Linux using a variety of methods. ..

What is a Process?

Running programs like your web browser, background processes associated with your desktop environment, and Linux system services are all processes.

You can lump processes into two groups:

Foreground processes are ones that have been started or launched by a user. They may be in a terminal window, or they may be a graphical application. Background processes are all of the processes that are started automatically and don’t have any interaction with users. They don’t expect input from users nor do they present results or output to them. Background processes are things like services and daemons.

If the foreground processes are the front of theater staff and the actors, the background processes are the backstage “behind the scenes” team.

When processes misbehave or malfunction, they can hog too much CPU time, consume your RAM, or enter a tight computational loop and become unresponsive. Graphical applications can refuse to respond to mouse clicks. Terminal applications might never return you to the command prompt.

The Humane Answer

“Killing” a process just means “forcing the process to quit.” This may be necessary if the process is refusing to respond.

Linux provides the kill, pkill, and killall commands to allow you to do just that. These commands can be used with any type of process, graphical or command line, foreground or background.

The kill Command

To use kill, you must know the process ID (PID) of the process you wish to terminate. The ps command can be used to find the PID of a process.

To have ps search through all of the processes use the -e (all processes) option. Piping the output through less is advisable, there’s going to be quite a bit of it. Type ps, a space, -e, a space, | (a pipe character), another space and then type less. Press Enter to execute the command.

This will give you a process listing that looks similar to the below screenshot. You can search forward in less using the / key and you can search backward using the ? key.

To home in on the process you’re interested in, pipe the output from ps through grep and specify the name—or part of the name—of the process.

Once you have located the PID of the process you wish to terminate, pass it to the kill command as a parameter. To terminate the shutter process identified by the previous command, use this command:

The kill command is a silent assassin—it does not give you any feedback if it was successful.

The pkill Command

The pkill command allows you to kill a process—or processes—by name. You do not need to identify the process by PID. To use pkill you provide a search term that pkill uses to check against the list of running processes. Matching processes are terminated. So you need to be positive you’ve got that search term spelled correctly.

As a safety net, you can use the pgrep command before you use the pkill command. The pgrep command also accepts a search term. It will list the PID of each process that matches the search term. This is safe because pgrep will not issue any kill signal to the processes, and if you mistype the search term you will not kill another process by mistake. You can make sure you have the search term correctly thought out before you pass it to pkill.  Both pkill and pgrep treat the search term in the same way. Their treatment is so similar that they share the same man page.

Let’s suppose there is a process with “subq” in its name. We’ll use the ps -u dave | grep command to get a peek behind the curtain. You can see that “subq” will match that process and that process alone. That was just so you can see the full name of the process.

Let’s assume our user hasn’t done that; all they know is the process name contains the substring “subq.” They use pgrep to check that there is only one match to the search term. They then use that search term with pkill.

You can use pkill to kill several processes at once. Here the user runs pgrep to check how many processes Chrome has launched. They use pkill to kill them all. They then check with pgrep that they have all been removed.

If several processes with the same name are running, but you do not want to kill them all, you can use pgrep with the -f (command line) option to identify which process is which. A simple example would be two ping processes. You want to kill one of them but not the other. You can use their command lines to distinguish between them. Note the use of quotation marks to wrap the command line parameter.

The killall Command

Warning: In the Solaris and OpenIndiana operating systems the killall command will kill all the processes that belong to you. If are root or if you have issued sudo killall you will reboot your computer! During the research for this article, this behavior was confirmed with the latest version of OpenIndiana Hipster 2018.10.

The killall command operates in a similar way to the pkill command but with a specific difference. Instead of passing a search term to the command you must provide the exact process name.

You cannot provide a partial match to a process name; you must provide the entire process name, as shown:

The -y (younger than) option allows you to kill processes that have been running for less than a specified period. The period is given in numbers followed by one of these units:

s (seconds) m (minutes) h (hours) d (days) w (weeks) M (months, note, capital “M”) y (years)

To kill a process called ana that has just been launched and leave any older instances of ana running, you could use the following parameters with killall, if you’d reacted within two minutes:

The -o (older than) option allows you to kill processes that have been running for longer than a specified period. This command will kill all ssh connections that have been running for longer than a day:

Don’t Be Too Trigger Happy

These commands will allow you to identify and terminate errant processes with accuracy and safety correctly.

Always be cautious. First, make sure the process you’re about to kill is really the one you want. Second, double check— be careful and ensure the targeted process is the one you want to end. Proceed with terminating the process once you’re satisfied.