Interested in learning more about Linux administration? Explore these Linux training courses.
Learn how to kill errant processes in this tutorial from our archives.
Picture this: You’ve launched an application (be it from your favorite desktop menu or from the command line) and you start using that launched app, only to have it lock up on you, stop performing, or unexpectedly die. You try to run the app again, but it turns out the original never truly shut down completely.
What do you do? You kill the process. But how? Believe it or not, your best bet most often lies within the command line. Thankfully, Linux has every tool necessary to empower you, the user, to kill an errant process. However, before you immediately launch that command to kill the process, you first have to know what the process is. How do you take care of this layered task? It’s actually quite simple…once you know the tools at your disposal.
Let me introduce you to said tools.
The steps I’m going to outline will work on almost every Linux distribution, whether it is a desktop or a server. I will be dealing strictly with the command line, so open up your terminal and prepare to type.
Locating the process
The first step in killing the unresponsive process is locating it. There are two commands I use to locate a process: top and ps. Top is a tool every administrator should get to know. With top, you get a full listing of currently running process. From the command line, issue top to see a list of your running processes (Figure 1).
Figure 1: The top command gives you plenty of information.
From this list you will see some rather important information. Say, for example, Chrome has become unresponsive. According to our top display, we can discern there are four instances of chrome running with Process IDs (PID) 3827, 3919, 10764, and 11679. This information will be important to have with one particular method of killing the process.
Although top is incredibly handy, it’s not always the most efficient means of getting the information you need. Let’s say you know the Chrome process is what you need to kill, and you don’t want to have to glance through the real-time information offered by top. For that, you can make use of the ps command and filter the output through grep. The ps command reports a snapshot of a current process and grep prints lines matching a pattern. The reason why we filter ps through grep is simple: If you issue the ps command by itself, you will get a snapshot listing of all current processes. We only want the listing associated with Chrome. So this command would look like:
The aux options are as follows:
a = show processes for all users
u = display the process’s user/owner
x = also show processes not attached to a terminal
The x option is important when you’re hunting for information regarding a graphical application.
When you issue the command above, you’ll be given more information than you need (Figure 2) for the killing of a process, but it is sometimes more efficient than using top.
Figure 2: Locating the necessary information with the ps command.
Killing the process
Now we come to the task of killing the process. We have two pieces of information that will help us kill the errant process:
Which you use will determine the command used for termination. There are two commands used to kill a process:
kill – Kill a process by ID
killall – Kill a process by name
There are also different signals that can be sent to both kill commands. What signal you send will be determined by what results you want from the kill command. For instance, you can send the HUP (hang up) signal to the kill command, which will effectively restart the process. This is always a wise choice when you need the process to immediately restart (such as in the case of a daemon). You can get a list of all the signals that can be sent to the kill command by issuing kill -l. You’ll find quite a large number of signals (Figure 3).
Figure 3: The available kill signals.
Tasks in Linux are called processes. Every process has a unique process ID. To terminate one in Linux we can use the kill command. In this tutorial, we’ll show you how to kill a process in Linux, to improve your VPS management skills.
Sometimes, you might start a server or an application, forget about it, and need to shut it off. In such scenarios, we can use the kill command.
Below are a few examples where the kill command can be helpful:
- To stop any automated process
- To stop a process that has been started by accident
- To stop a process that consumes a lot of memory
- To force stop any process running in Linux
- To stop a background process
Apart from stopping a process, the kill command can provide several features. Like, send a signal to a process. By default, this is taken as a TERM signal which will terminate the process.
How to Show the Process ID in Linux
Kill commands lets you terminate a process by using a specific process ID, also known as a pid. To show a pid in Linux you can execute the following command:
This will list all the available processes with a pid. If you want to make your list more specific – add a grep command like this:
This will display all java processes running in the output.
How to Show All Kill Command Signals
There are multiple signals available in Linux which can be used to interrupt, terminate, or pause processes. The command can be used as below:
This command will display a manual page entry of the different kill signals with their names and corresponding numbers. While there are multiple signals available, in most cases we would use SIGKILL (9) and SIGTERM (15).
How to Kill a Process in Linux Using the Terminal
Now we’re ready to move on and learn all the different uses of the Kill Command. To follow along, access your VPS server using SSH.
Using the Kill Command with a PID
To kill a specific process with a PID use the following command:
Here 63772 is the pid for a process we want to terminate. Since no signal is specified this will be SIGTERM signal. Sometimes, this may not work; in that case, you may have to kill a process forcefully.
In such cases, you can use the command format as shown below:
Below is a sample command to forcefully kill the process:
Similarly, to kill using the shorter option you can use:
Replace 63772 with the relevant pid for the process to be terminated.
How to Kill Multiple Processes in Linux
With the same command, you can kill multiple processes. The syntax for this command would be:
Here’s an example showing how it would look in the real world:
How to Kill a Process in Linux Using the Pkill Command
Pkill is a flavor of the kill command where you can specify the process name or a pattern to find a process:
The above command will kill the chrome browser. You can also specify a partial name match in the command line such as:
However, this command carries a risk of sometimes killing the wrong process, especially when there are multiple processes with the same name.
You can check the list by using the complete process name:
The above command can be used when you know the complete name of the process.
You can check for matching processes by using a partial name:
This command will list the process with the corresponding process ID.
How to Kill a Process in Linux Using the Killall Command
The basic difference between killall and kill is that killall can terminate the process by name while the kill command uses the pid.
An example of such command is:
This is similar to pkill. However, killall does an exact name match, while pkill can do a pattern match. This is one of the reasons, killall is safer compared to pkill.
One more difference is the root package to which these commands belong. In Linux, killall belongs to the psmisc package. On the other hand, commands such as ps, top, kill, pkill belong to procps package.
Another difference is that killall can be customized to terminate processes based on timestamps. In case you want to kill a process that has been running for less than 40 minutes, then you can use:
You can similarly use the below options together with the killall command:
- s – seconds
- m – minutes
- h – hours
- d – days
- w -weeks
- M – months
- y – years
This covers the most important and useful kill commands. To further learn about this essential utility you can refer to the Linux manual. Good luck with your project, see you in the next tutorial!
Edward is Hostinger’s copywriter. He’s an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Mar 12, 2018
Linux apps run pretty well these days. Newer, better programs with excellent features that help us get the most out of our computers come out every day. Unfortunately, sometimes, these programs stop working. When your favorite Linux program locks up, clicking the close-button doesn’t work. Lockups are the worst and cause tons of problems that can get in the way of your workflow. That’s why in this article, we’ll be going over all the best ways to quickly kill programs from the Terminal in Linux. We’ll go over multiple commands, as well as some useful GUI tools to use as well.
SPOILER ALERT: Scroll down and watch the video tutorial at the end of this article.
By far the easiest way to quickly kill programs from the Terminal in Linux is with the pkill command. Pkill is useful because it doesn’t require that the average user know the specific PID (process ID number). Instead, you’ll be able to end a process by just typing the name. Here’s an example of pkill in action.
For most users, running a non-root pkill command will be enough to end a stubborn, frozen program. It isn’t always the case though, and sometimes you’ll need to flex some muscles. For example, if you’d like to kill a program that is running as the root user, or it’s own user, you’ll need to attach sudo to it.
Still, even by using the pkill tool to end the root program may not be enough. Sometimes, an application works in such a way that a simple kill command isn’t going to help. When this happens, you can use -9. Adding -9 on the end of pkill will usually end the program by force when all other options fail.
Using Pidof And kill
When the Pkill command fails, there’s another excellent alternative: kill. Kill functions about the same way as pkill, except it works based on process ID numbers, rather than just the name of the program (like pkill firefox, etc.). The downside to killing programs with the kill command is that you’ll need first to discover what the problematic program’s PID is before using the kill command. The way to do this is by using the pidof command. Here’s how it works.
After pidof completes its search for the correct ID, you’ll see the output in the terminal. For example:
We can then take the 2219 PID and use it to kill Firefox.
Keep in mind that if you’re looking to kill a root program, you’ll need to do:
Lastly, if an application refuses to stop, try using -9. It works similarly to pkill.
Kill Programs With Htop
Don’t want to use individual commands to kill unresponsive programs on Linux? Consider installing HTOP. Like Pkill and Kill, it’s still possible to use this tool over SSH and remotely. Since it runs in a terminal, it’s still beneficial when killing programs. Htop is an improved version of the TOP system management tool included on most Linux systems. It has a lot of enhancements, such as graphics for better reading of CPU/RAM usage, and processes.
HTOP has other improvements like allowing the user to scroll as much horizontally/vertically without fear of interrupting the tool. It also lets users kill any process they want without needing to know the process ID and shows several different ways to kill things.
Note: HTOP requires Ncurses to function correctly. Install “ncurses” by searching in your Linux distribution’s package management tool (if htop doesn’t install it for you).
Grab the latest version of Htop for your Linux distribution by opening up a terminal and using the package management tool to install “htop.” If you can’t find it (for some reason), consider visiting the official website, downloading the source code and building it yourself!
Launch the HTOP tool at any time by opening a terminal window, and entering the command “htop.” Alternatively, the HTOP tool can be run at any time by simply searching for “htop” in the application menu. There should also be a shortcut for it under “system” or something similar.
When HTOP is open, use the arrow key to select a process you’d like to manage. Press F9 to open the “Kill” menu. Sort processes with F6, and search for running programs with F3. Need help using the HTOP tool? Press the F1 button on your keyboard to bring up the help menu. Entering “man htop” in the terminal also works.
Lastly, to exit the HTOP tool entirely, just press the Q button on the keyboard. Using the F10 button also works to quit.
We should mention that if you force kill programs from the Terminal, there’s a chance you might lose your work. With a browser, you can probably recover tabs but for other programs you will have to rely on their own built-in recovery mechanisms. If there aren’t any, it’s possible you might lose some data.
Note: We have explained the procedure mentioned in this article on Ubuntu 20.04 LTS. More or less the same commands and procedures can be followed in previous versions of Ubuntu,
Using the System Monitor
The Gnome system monitor is a built-in GUI utility that can be used to kill a process in Linux OS. It allows to stop a process and then resume it with the Continue option. The end option allows terminating a process safely while the kill option forcefully terminates the program.
If System Monitor is not installed in your system, you can install it as follows:
To launch System Monitor, hit the super key and search it using the search bar at the top. When the search result appears as follows, hit Enter to open it.
In the System Monitor window, you will see a list of processes running in your system. In order to kill a process, right-click it, and select Kill option. It will kill the selected process instantly.
Kill process using the keyboard shortcuts in Terminal
Linux command line includes a number of useful keyboard shortcuts. Among them, following shortcuts can be used to kill a running process.
Ctrl+C: It sends SIGINT that terminates the running process
Ctrl+Z: It sends SIGSTP that suspends an application and send it to the background. However, it does not terminate the process. To view the stopped process, you can use the jobs command. Use fg command to bring the process to the foreground.
Ctrl+\: It sends SIGQUIT that terminates the process. It also creates a core dump file which can be used to debug the process.
Kill process using the xkill utility
Xkill allows killing a running program using the mouse cursor. It is GUI based utility that is pre-installed in most of the systems. If not already installed, you can install it as follows:
To close any program, simply type this in your command line Terminal:
Running the above command will turn your mouse cursor in to x shape. Now place the cursor on the program that you want to close and left-click on it.
Set shortcut for Xkill
You can set a shortcut for xkill that will allow you to immediately kill an application without the need of opening the Terminal and running the command.
To create a shortcut for xkill, open the Settings utility using the right-click menu from the desktop. Then open the Keyboard Shortcuts tab and click the + icon at the very bottom.
Then in the following dialog, name the shortcut and type xkill in the Command field and click Set Shortcut button.
Then set a custom shortcut of your choice and click the Add button.
Now whenever you need to kill an application, simply press the shortcut keys and you will be able to kill any open application in your system.
Kill process using the Kill commands
There are also some command-line ways used to kill the processes in Linux which includes kill, pkill, and killall.
To find which processes are currently running in your system, you can use the ps command with –A flag:
It will list all the currently running processes in your system.
Kill command can be used to kill a running process in Linux. The kill command is provided with a PID of a process to be killed.
To find the process ID of a running process, you can use ps –A command. Alternatively, you can pipe the output of ps with grep command to find the process ID of a specific process:
To find the process ID of arunning Firefox program, you can use:
Once you have found the PID of a specific process, you can kill it as follows:
The kill command sends a SIGTERM signal to the specified PID which asks the process to terminate after performing the necessary cleanup operation.
In some scenarios, running the kill command does not terminate the process. If this is the case, you will need to type “kill -9” followed by the PID:
Using the -9 option with kill command sends a SIGKILL signal which asks the process to terminate immediately without any cleanup operation.
Similar to kill command, pkill also sends a SIGTERM signal which allows terminating an unresponsive process. However, the good thing about pkill is that you do not have to provide the PID of a process in order to kill it. Instead, you can just provide the matching keyword related to the process.
For example, to kill Firefox program, you can just type:
It will kill all the processes whose names match with the mnentioned .
With pkill, you also have an option to kill the process running by a specific user:
Be careful when using this option as If you do not specify the , all the processes with the specified username will be killed.
Killall command is similar to pkill except it takes the full process name as an argument instead of any matching keyword.
In order to use killall to terminate all the processes and their child processes with a specific name, use the following syntax:
That is all there is to it! In this article, you have learned various ways to kill a process in Linux. Use these commands with care as killing a process causes it to end immediately resulting in a data loss. Also killing the wrong process might end up disturbing the system.
About the author
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.
At times, you may feel as though some unnecessary processes are running on your system, you might not need a process any longer, or a process may have started malfunctioning. In all of these situations, you will have to try to find a way of killing (or ending) the processes. You can kill a process in Linux either via the GUI or the CLI. This article explores some of the different ways in which to kill a process from the command line in Linux.
Note: The methods shown in this article are performed in Linux Mint 20.
Methods for Killing a Process using the Command Line
The following include three distinct forms of killing a process from the command line in Linux Mint 20.
Method # 1: Using the “kill” Command
To kill a process via the terminal using the “kill” command in Linux Mint 20, proceed according to the method given below:
First, launch the terminal by clicking on its icon present in the Linux Mint 20 taskbar. The Linux Mint 20 terminal is shown in the following image:
To use the “kill” command, you will need to know the process ID of the process that you want to kill using this command. This is also known as the PID, which is a unique identifier for every process operating on your system. To determine the process IDs of all currently running processes on your system, execute the following command in the terminal:
This command will fetch a list of all the processes that are currently running on your Linux Mint 20 system, along with their PIDs.
The list of all currently running processes on my system is shown in the image below:
Now, after identifying the PID of the process that you want to kill, you will need to execute the “kill” command. For this scenario, we will kill the Firefox process with the PID “1934,” as you can verify from the image shown above.
Note that this PID will be different every time you run a new instance of a process. Moreover, in the image shown above, you can see multiple PIDs corresponding to the Firefox process. All of these are child PIDs, except for the first one. This is the reason behind using “1934” as the PID of the Firefox process, which is, in fact, its first PID.
The syntax of the “kill” command is as follows:
Here, you will need to replace the term “PID” with the PID of the process that you want to kill. In this example, we have replaced the term “PID” with “1934,” i.e., the PID of the Firefox process, as shown in the image below:
The successful execution of this command will immediately kill the Firefox process and you will no longer be able to find it in the list of currently running processes on your system.
Method # 2: Using the “pkill” Command
To kill a process via the terminal using the “pkill” command in Linux Mint 20, proceed according to the method given below:
The “pkill” command does not require the process ID of a process to kill it; rather, you can perform this action simply if you know the exact name of the process. In this scenario, we want to run the “ping” command, and then, during its execution, we want to kill the process using the “pkill” command.
First, ping a website using the following command:
Here, replace the URL with the website URL that you want to ping. In this example, we wanted to ping Google.com, as shown in the following image:
As this “ping” command is executing in the terminal, launch another instance of the terminal, and then, execute the following command in the terminal:
Here, replace the term “ProcessName” with the name of the process that you want to kill using the “pkill” command. In this example, we have replaced the term with “ping,” since we want to kill the ping process.
After hitting the Enter key, go back to the terminal instance where the “ping” command was running, and you will notice that this process has been terminated, as shown in the image below:
Method # 3: Using the “killall” Command:
To kill a process via the terminal using the “killall” command in Linux Mint 20, proceed according to the method given below:
The “killall” command can kill a process, along with all of its child processes. Again, just like the “pkill” command, the “killall” command does not require the PIDs; rather, you can simply kill a process if you know its name.
To demonstrate the usage of this command, we have replicated the same scenario again that we created above, i.e., we tried to ping Google.com with the “ping” command.
After doing that, launch a new terminal instance, and then, enter the following command:
Here, you can replace the term “ProcessName” with the name of the process that you need to kill using the “killall” command. In this case, we have replaced the term with “ping,” as shown in the image below:
After executing this command, you must go back to the terminal instance where you executed the “ping” command. You will notice that the process has already been terminated, as you can see in the following image:
By using any of the three methods described in this article, you can kill as many processes as you want while using Linux Mint 20, and hence, save all the system resources that these processes were occupying.
About the author
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.
This post was last updated on August 1st, 2020 at 12:44 pm
In Linux, a process is an instance of a program that is running on a computer. When a program starts executing it can have multiple processes associated with it. These processes can either run by a user or by the operating system itself. Usually, a process terminates on its own when they are done with their task.
Sometimes a process hanged up and starts consuming a lot of system resources such as CPU or RAM. Linux provides kill command to terminate such a process from the system. Ahead in this article, we will discuss finding and killing a process in Linux or Unix.
Process Identifier (PID)
The process identifier or PID is a number that is used by an operating system to identify an active process uniquely. PID can be used for tasks such as changing the priority of execution or killing processes altogether.
List the running processes in a system –
You can use top utility to see a list of running processes in a system with some other information regarding resource utilization and the priority of a process. Use the following command in your terminal –
This will display like given in the image below. Here you can identify a process with its process id if you want to kill it.
Or you can find the process id of a process by its name. To find PID use the following command in your terminal –
Or to find the process id of firefox use –
You can see the output of the pidof command in the given images.
Linux processes and signals
A signal is a notification, a message sent by either the operating system or some application to a program. In Linux when a process is killed a terminating signal is delivered to the process. Linux kernel implements about 30 signals, each of them is identified by a number, from 1to 31. Some commonly used signals are given below –
- SIGHUP(1) – Hangup a process. Use it to reload configuration files or open and close log files.
- SIGKILL(9) – Kill signal. Use it to forcefully kill a process.
- SIGTERM(15) – Termination signal. It is the default and safest way to kill a process.
- SIGSTOP(17,19,23) – Stop process
How to kill a process in Linux?
There are various commands such as kill, pkill, killall, etc that you can use to kill a process in Linux. The kill command is very straightforward all you need to have PID to kill a process. Now run the following command –
Here process id of vlc is 5402. To kill this process use the following command in your terminal –
To kill vlc forcibly use –
How to use pkill command to terminate a process Linux?
The pkill command allows you to kill the processes based on partial matches. Suppose you want to kill the processes containing the name vlc in their name. Then use the following command in your terminal –
To forcibly kill all the processes associated with vlc use-
How to use the killall command to kill the processes?
This is one of the easiest ways to kill a process from the terminal. Now if you know the exact name of the process then you can use the following command in your terminal –
Or to kill it forcibly use –
In this article, you learned to kill unwanted processes. Now if you have any queries related to this topic then you can write to us in the comments below.
Here are several options for terminating a program in Linux using the command line or a graphical interface.
Get the highlights in your inbox every week.
When a process misbehaves, you might sometimes want to terminate or kill it. In this post, we’ll explore a few ways to terminate a process or an application from the command line as well as from a graphical interface, using gedit as a sample application.
Using the command line/termination characters
Ctrl + C
One problem invoking gedit from the command line (if you are not using gedit & ) is that it will not free up the prompt, so that shell session is blocked. In such cases, Ctrl+C (the Control key in combination with ‘C’) comes in handy. That will terminate gedit and all work will be lost (unless the file was saved). Ctrl+C sends the SIGINT signal to gedit . This is a stop signal whose default action is to terminate the process. It instructs the shell to stop gedit and return to the main loop, and you’ll get the prompt back.
Ctrl + Z
This is called a suspend character. It sends a SIGTSTP signal to process. This is also a stop signal, but the default action is not to kill but to suspend the process.
It will stop (kill/terminate) gedit and return the shell prompt.
Once the process is suspended (in this case, gedit ), it is not possible to write or do anything in gedit . In the background, the process becomes a job. This can be verified by the jobs command.
jobs allows you to control multiple processes within a single shell session. You can stop, resume, and move jobs to the background or foreground as needed.
Let’s resume gedit in the background and free up a prompt to run other commands. You can do this using the bg command, followed by job ID (notice  from the output of jobs above.  is the job ID).
This is similar to starting gedit with &, :
kill allows fine control over signals, enabling you to signal a process by specifying either a signal name or a signal number, followed by a process ID, or PID.
What I like about kill is that it can also work with job IDs. Let’s start gedit in the background using gedit & . Assuming I have a job ID of gedit from the jobs command, let’s send SIGINT to gedit :
Note that the job ID should be prefixed with % , or kill will consider it a PID.
kill can work without specifying a signal explicitly. In that case, the default action is to send SIGTERM , which will terminate the process. Execute kill -l to list all signal names, and use the man kill command to read the man page.
If you don’t want to specify a job ID or PID, killall lets you specify a process by name. The simplest way to terminate gedit using killall is:
This will kill all the processes with the name gedit . Like kill , the default signal is SIGTERM . It has the option to ignore case using -I :
To learn more about various flags provided by killall (such as -u , which allows you to kill user-owned processes) check the man page ( man killall )
Have you ever encountered an issue where a media player, such as VLC, grayed out or hung? Now you can find the PID and kill the application using one of the commands listed above or use xkill .
xkill allows you to kill a window using a mouse. Simply execute xkill in a terminal, which should change the mouse cursor to an x or a tiny skull icon. Click x on the window you want to close. Be careful using xkill , though—as its man page explains, it can be dangerous. You have been warned!
Refer to the man page of each command for more information. You can also explore commands like pkill and pgrep .
My application runs as a background process on Linux. It is currently started at the command line in a Terminal window.
Recently a user was executing the application for a while and it died mysteriously. The text:
was on the terminal. This happened two times. I asked if someone at a different Terminal used the kill command to kill the process? No.
Under what conditions would Linux decide to kill my process? I believe the shell displayed “killed” because the process died after receiving the kill(9) signal. If Linux sent the kill signal should there be a message in a system log somewhere that explains why it was killed?
14 Answers 14
If the user or sysadmin did not kill the program the kernel may have. The kernel would only kill a process under exceptional circumstances such as extreme resource starvation (think mem+swap exhaustion).
Where -B100 signifies the number of lines before the kill happened.
Omit -T on Mac OS.
This looks like a good article on the subject: Taming the OOM killer.
The gist is that Linux overcommits memory. When a process asks for more space, Linux will give it that space, even if it is claimed by another process, under the assumption that nobody actually uses all of the memory they ask for. The process will get exclusive use of the memory it has allocated when it actually uses it, not when it asks for it. This makes allocation quick, and might allow you to “cheat” and allocate more memory than you really have. However, once processes start using this memory, Linux might realize that it has been too generous in allocating memory it doesn’t have, and will have to kill off a process to free some up. The process to be killed is based on a score taking into account runtime (long-running processes are safer), memory usage (greedy processes are less safe), and a few other factors, including a value you can adjust to make a process less likely to be killed. It’s all described in the article in a lot more detail.
Edit: And here is another article that explains pretty well how a process is chosen (annotated with some kernel code examples). The great thing about this is that it includes some commentary on the reasoning behind the various badness() rules.
Killing a process is sometimes the only way to get rid of it. Despite the harsh name, “killing” a process just means “forcing it to quit.” Here’s how to do it from the Linux or macOS command line.
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:
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.