Kill Command

Kill Command

[#quick-reference]Quick Reference[#quick-reference]

 # kill process
 $ kill 
 # hangup process
 $ kill -1 
 # terminate process
 $ kill -15 

[#which-version-of-kill]Determining which version of [.inline-code]kill[.inline-code] your shell is using[#which-version-of-kill]

When working with [.inline-code]kill[.inline-code], the first step is to make sure your shell is running the code you think it is. Both Bash and Zsh shells have their own built-in version of [.inline-code]kill[.inline-code] that may take precedence over the Linux binary [.inline-code]kill[.inline-code]. So you may need to run [.inline-code]/bin/kill[.inline-code] instead of [.inline-code]kill[.inline-code] in order to utilize the standard-library Linux binary [.inline-code]kill[.inline-code]; however, the functionality of the shell built-in [.inline-code]kill[.inline-code] will likely be largely the same.

For the rest of this article we will use the syntax [.inline-code]kill …[.inline-code], and assume that we are referring to Linux [.inline-code]kill[.inline-code]. To see which one will be run when you run a [.inline-code]kill[.inline-code]  command, you can run [.inline-code]which kill[.inline-code] as follows:

[#structure-of-kill]Structure of a [.inline-code]kill[.inline-code] command[#structure-of-kill]

The Linux [.inline-code]kill[.inline-code] command has the following structure:

 kill [OPTIONS] [PID]...

[#kill-signals-available][.inline-code]kill[.inline-code] signals available[#kill-signals-available]

“Options” in the structure above has a number of components, with the most commonly used option being the [.inline-code]signal[.inline-code] flag. To see a list of the signals available we can run the following in our shell:

 kill -l

Each of these signals is also associated with a given number, as again defined in the Linux docs. Depending on the configuration of your shell, you may also be able to run [.inline-code]kill -L[.inline-code] to see both the signal names and numbers.

Either the signal name or the signal number can be combined with the [.inline-code]kill[.inline-code] command as follows:

 $ kill -9 
 $ kill -s SIGKILL 
 $ kill -SIGKILL 
 $ kill 

The signal here ([.inline-code]SIGKILL[.inline-code]) happens to be the default signal, and will also be used if no specific signal is passed.

[#kill-to-check-user-access-to-process][.inline-code]kill[.inline-code] to check user access to a specific process[#kill-to-check-user-access-to-process]

[.inline-code]SIGNULL (0)[.inline-code], checks user access to or validity of a pid without sending an action signal.

 $ kill -0 
 $ kill -s SIGNULL 
 $ kill -SIGNULL 

[#terminate-process-gently][.inline-code]kill[.inline-code] to terminate a process gently[#terminate-process-gently]

[.inline-code]SIGTERM[.inline-code] is the “normal” kill signal. The application will be given time to shut down cleanly (save its state, free resources such as temporary files, etc.), and an application that is programmed to not immediately terminate upon a [.inline-code]SIGTERM[.inline-code] signal may take a moment to be terminated.

 $ kill -15 
 $ kill -s SIGTERM 
 $ kill -SIGTERM 

[#kill-force-termination][.inline-code]kill[.inline-code] to force termination[#kill-force-termination]

[.inline-code]SIGKILL[.inline-code] can be used to force termination and the application is not given a chance to respond.

 $ kill -9 
 $ kill -s SIGKILL 
 $ kill -SIGKILL 

[#determine-pid-of-process]Determining the PID of a process[#determine-pid-of-process]

The second part of the [.inline-code]kill[.inline-code] command is the process ID, or “pid”. There are a number of ways we can determine which processes are running and their pid’s, including [.inline-code]ps[.inline-code], [.inline-code]pidof[.inline-code], and [.inline-code]pgrep[.inline-code] and [.inline-code]top[.inline-code]. Which to use will depend on exactly what you are trying to do.

For example, to get the [.inline-code]pid[.inline-code] of all processes related to Google Chrome you can run the following:

 $ pidof chrome

Alternatively, to list all processes running on your machine, consider [.inline-code]ps aux[.inline-code] or [.inline-code]top[.inline-code]:

[#kill-9-1][.inline-code]kill -9 -1[.inline-code] terminates all running processes[#kill-9-1]

However, this should be done very carefully; this can cause damage to your kernel depending on what is running on your machine at the time.

[#kill-with-pidof]Combining [.inline-code]kill[.inline-code] with [.inline-code]pidof[.inline-code][#kill-with-pidof]

[.inline-code]kill[.inline-code] is most powerful when combined with other terminal commands. For example, to identify and kill all processes related to Google Chrome we can run the following command:

 kill $(pidof chrome)

[#kill-with-sudo]Overcoming [.inline-code]kill[.inline-code] permission issues with [.inline-code]sudo[.inline-code][#kill-with-sudo]

If any of [.inline-code]kill[.inline-code] commands fail one reason might be lack of permissions. Simply re-run the command using [.inline-code]sudo[.inline-code] and enter your password in order to proceed:

 # Careful! Using `sudo` can be dangerous when 
 # dealing with system processes
 sudo kill $(pidof chrome)