Kill Command

Tom Wagner
Tom WagnerSoftware Engineer, Pinterest
Published: February 1, 2024

Quick Reference

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

Determining which version of kill your shell is using

When working with kill, 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 kill that may take precedence over the Linux binary kill. So you may need to run /bin/kill instead of kill in order to utilize the standard-library Linux binary kill; however, the functionality of the shell built-in kill will likely be largely the same.

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

Structure of a kill command

The Linux kill command has the following structure:

Bash
kill [OPTIONS] [PID]...

kill signals available

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

Bash
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 kill -L to see both the signal names and numbers.

Either the signal name or the signal number can be combined with the kill command as follows:

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

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

kill to check user access to a specific process

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

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

kill to terminate a process gently

SIGTERM 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 SIGTERM signal may take a moment to be terminated.

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

kill to force termination

SIGKILL can be used to force termination and the application is not given a chance to respond.

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

Determining the PID of a process

The second part of the kill 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 ps, pidof, and pgrep and top. Which to use will depend on exactly what you are trying to do.

For example, to get the pid of all processes related to Google Chrome you can run the following:

Bash
$ pidof chrome

Alternatively, to list all processes running on your machine, consider ps aux or top:

kill -9 -1 terminates all running processes

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.

Combining kill with pidof

kill 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:

Bash
kill $(pidof chrome)

Overcoming kill permission issues with sudo

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

Bash
# Careful! Using `sudo` can be dangerous when 
 # dealing with system processes
 sudo kill $(pidof chrome)
Written by
Tom Wagner
Tom WagnerSoftware Engineer, Pinterest
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.