How to Kill Processes From the Linux Terminal

Stylized Linux terminal on the Ubuntu theme

Killing a process is sometimes the only way to get rid of it. Despite its tough name, "killing" a process simply means "forcing it to leave". Here's how to do it from the Linux or macOS command line.

What is a process?

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

You can group processes into two groups:

Leading process are those that have been started or started by a user. They can be in a terminal window or be a graphics application.
Process of the background are all processes that are started automatically and have no interaction with users. They do not expect user feedback, nor do they present results or results. Background processes are things like services and daemons.

While leading processes involve theater staff and actors, background processes are the behind-the-scenes behind-the-scenes team.

When processes behave poorly or work poorly, they can accumulate too much CPU time, consume RAM or enter a tight computing loop and stop responding. Graphics applications may refuse to respond to mouse clicks. Terminal applications may never return you to the command prompt.

The human response

"Killing" a process simply means "forcing the process to quit". This may be necessary if the process refuses to respond.

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

The kill command

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

For ps to search in all processes, use the -e (all processes) option. It is advisable to use less output, there will be a good part. Type ps, a space, -e, a space, | (a pipe character), another space and type less. Press Enter to execute the command.

ps -e | less

ps command in a terminal window

This will give you a list of processes that looks like the screen capture below. You can search before using the / key and you can search backwards with the? key.

ps output in a window less

To familiarize yourself with the process you are interested in, direct the output of ps by grep and specify the name – or part of the name – of the process.

ps -e | grep shutter

The ps command goes through grep to find the shutter process

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

kill 2099

Kill command in a terminal window

The kill command is a silent assassin – it gives you no return if it succeeds.

The pkill command

The pkill command allows you to kill one or more 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 the list of processes that are running. The matching processes are complete. You must therefore make sure that the search term has been spelled correctly.

As a safety net, you can use the pgrep command before using 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 sure because pgrep will not emit any stop signal to processes, and if you type the search term, you will not be able to kill another process by mistake. You can make sure that the search term is correctly thought before passing it to pkill. Pkill and pgrep treat the search term in the same way. Their treatment is so similar that they share the same manual page.

Suppose there is a process with "subq" in his name. We will use the ps -u dave | grep command to take a look behind the curtain. You can see that "subq" will match this process and this process alone. It was just so you could see the full name of the process.

ps -u dave | grep subq

ps command in a terminal window

Suppose our user did not do that; all they know is that the process name contains the substring "subq". They use pgrep to check that there is only one match with the search term. They then use this search term with pkill.

pgrep subq

pkill subq

pgrep and pkill in a terminal window

You can use pkill to kill multiple processes at once. Here, the user launches pgrep to check the number of processes launched by Chrome. They use pkill to kill them all. They then check with pgrep that they have all been deleted.

pgrep chrome
pkill chrome
pgrep chrome

pgrep and pkill in a terminal window

If multiple 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 matches which process. 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 them. Note the use of quotation marks to wrap the command line parameter.

pgrep -f "ping"
pkill -f "ping"

pgrep pkill with the ping command line

The killall command

Warning: In Solaris and OpenIndiana operating systems, the killall command removes all processes that belong to you. If you are root or if you have issued sudo killall, you will restart your computer! When searching for this article, this problem has been confirmed with the latest version of OpenIndiana Hipster 2018.10.

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

You can not provide a partial match to a process name; you must provide the full name of the process, as shown:

killall shutt
killall shutter

Killall command in a terminal window

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

s (seconds)
m (minutes)
h (hours)
d (days)
w (weeks)
M (month, note, capital letter "M")
y (years)

To delete a process called ana that has just been started and leave all previous instances of ana running, you can use the following parameters with killall if you have reacted within two minutes:

killall -y 2m ana

killall with younger than the option

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

killall -o 1d sshd

Killall command in a terminal window

Do not be too happy

These commands will allow you to correctly identify and terminate errant processes with precision and security.

Always be careful. First, make sure the process you are about to kill is the one you want. Second, double check – be careful and make sure the targeted process is the one you want to complete. Complete the process once you are satisfied.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.