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 from the Terminal Command Line.
Steps To Kill a Process in Linux from Terminal
To complete this guide, you must have access to your server as a root or non-root user with sudo privileges. Then, follow the steps below to use the Linux Commands and start your killing process.
Step 1 – Locate the Command Process in the Linux Terminal
The first step to kill a process is locating it. There are two commands that are used 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
Step 2 – Kill or Remove a Process from Terminal
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 you want from the kill command.
These are the most common signals for kill commands:
HUNGUP=1 KILL=9 TERMINATE=15
Killing Command Process with a Specific ID
For example, when we want to kill a process in Linux with a specific ID, we use the kill -9 PID method to kill it:
kill -9 5586
Killing Command Process with Name
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).
Killing a process with all its sub-processes
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.
At this point, you have learned to locate the command process and start to kill a process in Linux from the terminal by using the Linux Commands. Hope you enjoy using it.
You may also be interested in other content from the Linux Tutorials: