Two Commands to Kill Processes in Linux Operating System

Kill Processes in Linux

Linux processes sometimes will become sluggish or unresponsive. If a process refuses to close normally, you may need to kill that process. In this tutorial, we will look at some of the command-line tools that let us terminate misbehaving processes.

The two commands we are going to look at are kill and killall.

Kill command - Send a signal to a process

The kill command is simple and easy to use. The command doesn’t exactly kill processes, but sends them signals.

The syntax of the kill command is as follows:

kill -signal_number PID

The signal switch is optional. If you don’t give a signal number, you send the termination signal, which is SIGTERM, which has the signal ID 15.

If the process refuses stubbornly to terminate, then terminate the process forcefully, using the signal ID 9 (SIGKILL).

kill -9 PID

The kill -9 terminates the process immediately, therefore it won't have enough time to close files that were open by the process. Hence it is not recommended to use kill -9 unless it is a must, instead use SIGTERM (15). The only difference is that it might take a little bit longer to terminate the process.

Commonly Used Kill Signals

Kill SignalIDDescription
SIGHUP1Known as the Hangup signal. SIGHUP will stop the designated process and restarts it with the same process ID.
SIGINT2A weak kill signal. It will usually terminate the program, but isn’t guaranteed to work.
SIGTERM15This is the default kill signal. It terminates the process.
SIGKILL9Immediately terminates the process. A very strong signal that should be used only as a last resort if SIGTERM is not working.

How do I find the process ID?

The kill command works on the process ID, to find the PID of a process run the ps aux command.

Two Commands to Kill Processes in Linux Operating System

This will provide you the list of all running processes. To be more specific, you can filter the output with the grep command. For example, run the following command to find the PID of the SSH Server process.

ps aux | grep sshd
Kill command - Send a signal to a process

Killall (Kill All Processes by A Name)

The killall command terminates all processes that match the given name. For example, To kill all running instances of the Google Chrome browser on your Linux desktop, you enter the following command:

killall chrome

However, it is unsafe to run the killall command without the -e option, which stands for exact match and requires the process name to match exactly.

killall -e chrome

Another useful command option is -u, which allows us to specify the user name so that only the processes belonging to that user will be terminated. For example, you can run the following command to kill all MySQL processes belonging to the mysql user.

killall -u mysql

Just like the kill command, if no signal is specified, a default of 15 is used.