How to Kill Processes in Unix/Linux
How to decide which kill command to use to best terminate a process
Join the DZone community and get the full member experience.Join For Free
There are different options to terminate a process in Unix/Linux flavor of operating systems. This article intends to list and provide examples of each option.
You can use the kill command to terminate a process by passing the process id. PID is the process ID of the process that you want to terminate.
If you want to kill a process whose PID is 1692 then the command would be:
Sometimes when you issue the kill command you may get an ‘operation not permitted’ error message. In those circumstances, issue the kill command with a ‘-9’ signal.
If you continue to get an ‘operation not permitted’ error message even with the ‘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 superuser) 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 command is another option to terminate multiple processes in one stroke. You can use this option to kill processes based on their names or by the users who launched them. Let’s explore all killall options in this section.
- killall process_name: Kill processes that match the specified process name. For example, refer to line 2 if you want to kill the process whose process name is ‘java.'
Fig: killall processname
Note: If 3 processes are running with the name ‘java’, then when you use killall, all 3 processes will be terminated.
- killall -I process_name: Ignore case when trying to find the process name. 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. Refer to the example on line 2.
- killall -i process_name: Ask for additional confirmation when killing the process. If you want to kill the process whose process name is ‘java’ and the system requires confirmation, refer to the example on line 2.
Fig: Killall Confirmation
- killall -u username: Kills processes owned by a specific user. Line 1 demonstrates this. If you want to kill all the processes owned by the username ‘ec2-dev’, refer to the example on line 2.
- killall -v process_name: Report back on whether the process has been successfully killed.
Fig: Process Killed Reported
You can use the pkill command to terminate processes by sending a full name or partial name of the process. By default, pkill will send the signal SIGTERM. If you want to kill the process whose process name is ‘java’, refer to the example on line 2.
The Difference Between ‘killall’ and ‘pkill’
killall will look for the exact match of the process name whereas pkill will allow terminating the process either by full name or by partial process name.
Example: There is a process with the name ‘java’. If you try to terminate the process using ‘killall’ command by giving a partial name, i.e. ‘ava’, then you will get a ‘no process found’ error:
Fig. killall using partial process name
On the other hand if you try to terminate the process using the pkill command by giving a partial name, i.e., ‘ava’, it will succeed:
Fig. pkill using partial process name
You can also terminate a process using the popular system monitoring tool – top. Below are the steps to terminate a process in top:
- Press the ‘k’ key when the top command is running
- You will be prompted to enter the PID you want to terminate. Type the PID.
- You will be prompted to enter which signal you want to use to kill the process. Type '9' which is a SIGKILL. By default, the signal will be SIGTERM ('15').
- Press enter
Fig: killed using SIGKILL signal
Opinions expressed by DZone contributors are their own.