Apr 28, 2022
Edward S.
3min Read
Tasks in Linux are called processes. Every process has a unique process ID. In this tutorial, we’ll show you how to terminate a process using Linux commands, to improve your VPS management skills.
Download Complete Linux Commands Cheat Sheet
- What Is Kill Command Used For?
- How to Show the Process ID in Linux
- How to Show All Kill Command Signals
- How to Kill a Process in Linux Using the Terminal
- Using the Kill Command with a PID
- How to Kill Multiple Processes in Linux
- How to Kill a Process in Linux Using the Pkill Command
- How to Kill a Process in Linux Using the Killall Command
What Is Kill Command Used For?
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:
psThis will list all the available processes with a pid. If you want to make your list more specific – add a grep command like this:
ps -ux | grep javaThis 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:
kill -lThis 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 virtual private server using SSH.
Using the Kill Command with a PID
To kill a specific process with a PID use the following command:
kill 63772Here 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:
kill SIGKILL 63772Similarly, to kill using the shorter option you can use:
kill -9 63772Replace 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:
kill -9 pid1 pid2 pid3Here’s an example showing how it would look in the real world:
kill -9 63772 45116 23465How 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:
pkill chromeThe above command will kill the chrome browser. You can also specify a partial name match in the command line such as:
pkill chrHowever, 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:
pidof chromeThe 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:
pgrep -l chrThis 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:
killall chromeThis 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:
killall -y 40m [Process_Name>]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
Conclusion
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 a Content Editor with years of experience in IT as a writer, marketer, and Linux enthusiast. Edward's goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.