One of the major advantages of Linux is the ability to terminate a process without having to restart the server. In this article, we will teach you how to kill a process in Linux.
How to Kill a Process in Linux
Locating the process
The first step to kill a process is locating it. There are two commands that use to locate a process: top and ps.
The Top is a tool that every administrator should get to know. With the top command, you get a full listing of the currently running process. From the command line, issue top to see a list of your running processes. Then stay with us for the rest of the article we show you how to kill a process in Linux.
From this list, you will see some rather important information.
Or 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. For example, we only want the listing associated with MySQL. So this command would look like this:
ps aux | grep mysql
The aux options are as follows:
a = show processes for all users
u = display the process’s user/owner
x = also shows processes not attached to a terminal
How to Kill the process
We have two pieces of information that will help us to kill a process in Linux:
1. Process name
2. Process ID
Which you use will determine the command used for termination. There are two commands used to kill a process in Linux:
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 in you want from the kill command.
These are the most common signals for kill commands:
HUNGUP=1 KILL=9 TERMINATE=15
For example, when we want to kill a process in Linux with a specific ID, we use the kill -9 PID methods to kill it:
kill -9 5586
If you want to use the process name instead of its PID to kill the process in Linux, you can use the pkill command.
For example, to kill a process called MySQL, you can use this command:
The previous two commands could be used to kill only one process. But with the killall command, a process can be killed with all its sub-processes (children).
For example, we use this command to kill the MySQL command and all its sub-processes:
These are the most common examples of killing a process in Linux.
We hope this article about how to kill a process in Linux has been useful for you.
You may also be interested in other content from the Linux Tutorials: