There are different options to terminate a process in Unix/Linux flavour of operating systems. This article intends to list down those options.
kill
You can use the ‘kill’ command to terminate a process by passing the ‘process id’.
kill {PID}
PID – is the ‘process Id’ of the process that you want to terminate. If you are not sure how to find ‘process Id’, refer to this article.
Example:
Suppose if you want to kill a process whose ‘process Id’ is 1692 then command would be:
kill 1692
Sometimes when you issue the ‘kill’ command you may get an ‘operation not permitted’ error message. In those circumstances, issue kill command with ‘-9’ signal. i.e.
kill -9 {PID}
kill -9 1692
If you continue to get an ‘operation not permitted’ error message even with ‘kill -9’ signal then you may not have sufficient permission to terminate the process. Then you need to ‘sudo’ as a ‘root’ user (i.e. login as a super user) and then issue the ‘kill -9’ command.
NOTE: This is the same as the ‘run as administrator’ option in Windows.
Fig: Terminating the process with ‘kill -9’ signal
killall
‘killall’ command is another option to terminate multiple processes in one stroke. You can use this option to kill processes based on their names, users who launched them. Let’s explore all ‘killall’ options in this section.
a. killall process_name: Kill processes that match the specified process name.
killall {ProcessName}
Suppose if you want to kill the Process whose ‘process name’ is ‘java’, then command would be:
Fig: killall processname
NOTE: Say if 3 processes are running with the name ‘java’, then when you use ‘killall’, all the 3 processes will be terminated.
b. killall -I process_name: Ignore case when trying to find the process name.
killall -I {ProcessName}
Suppose if you want to kill the Process whose ‘process name’ is ‘java’, but if you are not sure about the case sensitivity of the process, then you can use ‘-l’ option:
killall -I JAVA
c. killall -i process_name: Ask for additional confirmation when killing the process.
killall -i {ProcessName}
Suppose if you want to kill the Process whose ‘process name’ is ‘java’, it needs to ask for confirmation, then command would be:
Fig: Killall Confirmation
d. killall -u username: It kills processes owned by a specific user.
killall -u {UserName}
Suppose if you want to kill all the Process owned by the username ‘ec2-dev’, then command would be:
killall -u ec2-dev
e. killall -v process_name: Report back on whether the process has been successfully killed.
killall -v {ProcessName}
Suppose if you want to kill the Process whose ‘process name’ is ‘java’ and you wanted to know whether the process is successfully killed, then command would be:
Fig: Process Killed Reported
pkill
You can use ‘pkill’ command to terminate process(es) by sending a fullname or partial name of the process. By default pkill will send the signal SIGTERM.
pkill {ProcessName}
Suppose if you want to kill the Process whose ‘process name’ is ‘java’, then command would be:
pkill java
Difference between ‘killall’ and ‘pkill’
‘killall’ will look for the exact match of the process name whereas the ‘pkill’ will allow to terminate the process either by full name or partial process name.
EXAMPLE: There is a process with the name ‘java’. If you try to terminate the process using ‘killall’ command by giving partial name i.e. ‘ava’ then you will get ‘no process found’ error:
Fig. killall using partial process name
On the other hand iIf you try to terminate the process using ‘pkill’ command by giving partial name i.e., ‘ava’, it will succeed:
Fig. pkill using partial process name
top
You can also terminate a process using the popular system monitoring tool – ‘top’. Below are the steps to terminate a process in ‘top’:
1. Press ‘k’ key when top command is running
2. It will ask you to enter the Process Id which you want to terminate
3. Type the Process Id
4. Then it will ask you the signal you want to use for killing the Process
5. Type ‘9’ which is a SIGKILL. By default the signal will be SIGTERM (‘15’). If you would like to know the difference between SIGKILL and SIGTERM, refer to the below section (‘Difference between SIGTERM, SIGKILL’) in this article.
6. press enter
Fig: killed using SIGKILL signal