How to Use the kill Command in Linux

Updated on

5 min read

Kill Command

Linux is a great and advanced operating system, but it is not perfect. Once in a while, some applications may start behaving erratically and become unresponsive or start consuming a lot of system resources. Unresponsive applications cannot be restarted because the original application process never shuts down completely. The only solution is to either restart the system or kill the application process.

There are several utilities that allow you to terminate errant processes, with kill being the most commonly used.

kill Command

kill is a shell built-in in most Bourne-derived shells such as Bash and Zsh. The command behavior is slightly different between the shells and the standalone /bin/kill executable.

Use the type command to display all locations on your system containing kill:

Terminal
type -a kill
output
kill is a shell builtin
kill is /bin/kill

The output above shows that the shell built-in has priority over the standalone executable, and it is used whenever you type kill. If you want to use the binary, type the full path to the file /bin/kill. In this article, we will use the Bash built-in.

The syntax of the kill command takes the following form:

Terminal
kill [OPTIONS] [PID]...

The kill command sends a signal to specified processes or process groups, causing them to act according to the signal. When the signal is not specified, it defaults to -15 (-TERM).

The most commonly used signals are:

  • 1 (HUP) - Reload a process.
  • 9 (KILL) - Kill a process.
  • 15 (TERM) - Gracefully stop a process.

To get a list of all available signals, invoke the command with the -l option:

Terminal
kill -l
kill-a-process-in-linux

Signals can be specified in three different ways:

  1. Using number (e.g., -1 or -s 1).
  2. Using the “SIG” prefix (e.g., -SIGHUP or -s SIGHUP).
  3. Without the “SIG” prefix (e.g., -HUP or -s HUP).

The following commands are equivalent to one another:

Terminal
kill -1 PID_NUMBER
kill -SIGHUP PID_NUMBER
kill -HUP PID_NUMBER

The PIDs provided to the kill command can be one of the following:

  • If PID is greater than zero, the signal is sent to the process with ID equal to the PID.
  • If PID is equal to zero, the signal is sent to all processes in the current process group. In other words, the signal is sent to all processes belonging to the GID of the shell that invoked the kill command. Use ps -efj command to view the process group IDs (GIDs).
  • If PID is equal to -1, the signal is sent to all processes with the same UID as the user invoking the command. If the invoking user is root, the signal is sent to all processes except init and the kill process itself.
  • If PID is less than -1, the signal is sent to all processes in the process group with GID equal to the absolute value of the PID.

Regular users can send signals to their own processes, but not those that belong to other users, while the root user can send signals to other users’ processes.

Terminating Processes Using the kill Command

To terminate or kill a process with the kill command, first you need to find the process ID number (PID). You can do this using different commands such as top, ps , pidof and pgrep .

For example, you can use ps to find the PID for a running process:

Terminal
ps aux | grep firefox

Let’s say the Firefox browser has become unresponsive, and you need to kill the Firefox process. To find the browser PIDs use the pidof command:

Terminal
pidof firefox

The command will print the IDs of all Firefox processes:

output
6263 6199 6142 6076

Once you know the PIDs, you can kill all of them by sending the KILL signal:

Terminal
kill -9 6263 6199 6142 6076

Instead of searching for PIDs and then killing the processes, you can combine the above commands into one:

Terminal
kill -9 $(pidof firefox)

Reloading Processes Using the kill Command

Another common use case for kill is to send the HUP signal, which tells the process to reload its settings.

For example, to reload Nginx , you need to send a signal to the master process. The process ID of the Nginx master process can be found in the nginx.pid file, which is typically located in the /var/run directory.

Use the cat command to find the master PID:

Terminal
cat /var/run/nginx.pid
output
30251

Once you find the master PID, reload the Nginx settings by typing:

Terminal
sudo kill -1 30251

The command above must be run as root or user with sudo privileges.

SIGTERM vs SIGKILL

When terminating processes, you should first try SIGTERM (-15), which allows the process to clean up and exit gracefully. If the process doesn’t respond, use SIGKILL (-9), which forces immediate termination.

Terminal
# Try graceful termination first
kill -15 PID

# If process doesn't stop, force kill
kill -9 PID

SIGKILL cannot be caught or ignored by the process, so it always works. However, it doesn’t allow the process to save data or clean up, which may cause data loss.

Check if a Process Exists

Use signal 0 to check whether a PID exists without sending a real signal:

Terminal
kill -0 PID

Kill Processes by Name

Instead of finding the PID first, you can kill processes by name using pkill or killall.

To kill all Firefox processes by name:

Terminal
pkill firefox

Or using killall:

Terminal
killall firefox

The difference is that pkill matches partial names by default, while killall requires the exact process name.

Killing Background Jobs

If you have background jobs running in the current shell, you can kill them using the job number instead of the PID:

Terminal
kill %1

To list all background jobs and their numbers, use the jobs command:

Terminal
jobs
output
[1]+  Running    sleep 1000 &
[2]-  Running    sleep 2000 &

Quick Reference

CommandDescription
kill PIDSend SIGTERM to process
kill -9 PIDForce kill process
kill -15 PIDGracefully terminate process
kill -1 PIDReload process configuration
kill -lList all signals
kill %1Kill background job number 1
pkill nameKill processes by name
killall nameKill all processes with exact name

Conclusion

The kill command is used to send signals to processes in Linux. Always try SIGTERM (-15) first for graceful termination, and use SIGKILL (-9) only when necessary.