To kill a running process, you must find out the process id or process name on the Linux console (Terminal). As an example we will find and kill the mysql service.
Step 1: View and locate the running process
“top” interface
The top command is used for real-time monitoring of running processes on Unix and Linux operating systems.
Usage:
[email protected]:~$ top
or you can use it with the grep command to search for a specific process.
[email protected]:~$ top | grep mysql
Output:
“ps” command
The “ps” command lists the processes currently running in the operating system. If you want the running processes to be displayed in real time, you can use the top command. The ps command can take with it three commonly used parameters a, u, and x. This command shows all running processes and process owners in the system and terminals.
Usage:
[email protected]:~$ ps aux
or you can use it with the grep command to search for a specific process.
[email protected]:~$ ps aux | grep mysql
Output:
“pgrep” command
“pgrep” is a command firstly written for the Solaris 7 operating system. It was later refactored for Unix and different BSDs. It prints the ID of the named processes.
Usage:
[email protected]:~$ pgrep mysql
Output:
“pidof” command
Pidof is an application that returns the IDs of running processes like pgrep.
Usage:
[email protected]:~$ pidof <process Name>
Sample:
[email protected]:~$ pidof mysql
Output:
We have obtained the ID (1111) and process name (mysql, mysqld) of the Mysql service, now let’s see how to kill/terminate this process.
Step 2: Killing process
On Linux, you can kill a process using the killall, pkill, kill, kill -9, xkill and top commands. Let’s see how to kill a process using these commands in Linux terminal.
“kill” command
“kill” is a command to stop processes running on Unix and Linux by signaling them.
Usage:
[email protected]:~$ kill <Process ID>
Sample:
[email protected]:~$ kill 1111
[email protected]:~$ _
“killall” command
killall is a command to kill the processes named.
Usage:
[email protected]:~$ killall <Process Name>
Sample:
[email protected]:~$ killall mysqld
[email protected]:~$ _
“pkill” command
“pkill” is a command that was also developed for the Solaris 7 operating system and is used to kill running processes by sending signals such as kill and killall commands.
Usage:
[email protected]:~$ pkill <Process Name>
Example:
[email protected]:~$ pkill mysqld
[email protected]:~$ _
“kill -9” command
Unlike the kill command, the kill -9 command is often used to definitively kill unresponsive processes. The kill command signals gracefully, while the kill -9 command is more vulgar at killing processes. Alternatively, it can be run with the -SIGKILL parameter instead of -9.
Usage:
[email protected]:~$ kill -9 <Process ID>
or
[email protected]:~$ kill -SIGKILL <Process ID>
Sample:
[email protected]:~$ kill -9 1111
[email protected]:~$ _
or
[email protected]:~$ kill -SIGKILL 1111
[email protected]:~$ _
“xkill” command
xkill is often used to force-kill GUI applications. It is quite useful in killing some unresponsive applications that cause the system to work abnormally.
Usage:
[email protected]:~$ xkill <Process ID>
Sample:
[email protected]:~$ xkill 1111
[email protected]:~$ _
“top” interface
You can kill running processes using the “Top” interface. Press the “k” key while running the “top” interface, enter the process ID and press “enter”.
Usage:
[email protected]:~$ top
Sample: