VOOZH about

URL: https://dzone.com/articles/how-to-kill-process-in-unixlinux

⇱ How to Kill Processes in Unix/Linux


Related

  1. DZone
  2. Coding
  3. Java
  4. How to Kill Processes in Unix/Linux

How to Kill Processes in Unix/Linux

How to decide which kill command to use to best terminate a process

Likes
Comment
Save
46.2K Views

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.

kill

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. 

Java




x


1
kill {PID}



If you want to kill a process whose PID is 1692 then the command would be:

Java




xxxxxxxxxx
1


1
kill 1692



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.

Java




xxxxxxxxxx
1


1
kill -9 {PID}
2
kill -9 1692



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

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.'
Java




xxxxxxxxxx
1


1
killall {ProcessName}
2
killall 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.
Java




xxxxxxxxxx
1


1
killall -I {ProcessName}
2
killall -I JAVA



  • 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.
Java




xxxxxxxxxx
1


1
killall -i {ProcessName}
2
killall -i java




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.
Java




xxxxxxxxxx
1


1
killall -u {UserName}
2
killall -u ec2-dev



  • killall -v process_name: Report back on whether the process has been successfully killed.
Java




xxxxxxxxxx
1


1
killall -v {ProcessName}
2
killall -v java



Fig: Process Killed Reported

pkill

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.

Java




xxxxxxxxxx
1


1
pkill {ProcessName}
2
pkill java



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

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 the ‘k’ key when the top command is running
  2. You will be prompted to enter the PID you want to terminate. Type the PID.
  3. 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').
  4. Press enter

Fig: killed using SIGKILL signal


Command (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Modify Java Command-Line Arguments
  • JGit Library Examples in Java
  • Quickly Find Your Java Application Process ID
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: