If you are a new Linux user of any distribution, you probably don't know how to use one of the essential commands, which is the one that helps us to terminate running processes.
The operating systems based on Unix incorporate the Kill command, useful to kill those processes that have been stuck or that we don't want to run anymore without having to restart our computer or server.
The Kill command sends different types of signals that allow terminating processes or groups of processes. If no signal type is specified, the TERM signal will be sent, which is the default option.
Note for intermediate users: The command can be external (located in /bin/kill) or internal (more recent shells) depending on the type of Linux shell. In any case, its use is very similar.
KILL Command Signal Types
The most common signal types are the following
- SIGHUP: It will be used if the console doesn't respond or if control over the process has been lost. This signal is used to reload the configuration files of the process, as well as the possible log files of the same.
- SIGKILL: Signal to stop a process. It is the last hope to terminate a process when it doesn't respond. It's not a clean way to end a process, and this is why no data will be backed.
- SIGTERM: It is the fastest way to close a process. It is the default signal.
Permissions Required To Terminate a Process
The permissions are the following:
- You can only terminate your own processes.
- The root user will be able to kill all processes, system process and from any other users.
KILL Command Syntax
The syntax is the following:
kill [options] [signal] PID
If you want to terminate several processes at the same time:
kill [options] [signal] PID1, PID2 ...
As you can see, first you need to type the command name, then its parameters, then the type of signal and finally the PID or the PID of the process that you want to terminate.
How-To Kill Command Examples
Let's see an example when you want to kill the Apache httpd process:
Step # 1: Find the PID of the process
The first thing you have to do when you want to terminate any process is to find their PID. For this, you can use the ps or pidof commands.
That command will give you the PID, on this case is 22298, which is the Process Identifier, although you could also have as a result of a list of identifiers since the process could have several instances.
You can also run the following command to find the PID:
ps aux | grep httpd
Step # 2: Kill Process using PID
The PID 22298 is the one assigned to the httpd process. To terminate the process and close the Apache server, you need to pass your PID:
This will end the process with the PID 22298, something you can check if you use the pidof command or the ps command again :
If we don't specify any signal. The signal SIGTERM (-15) will be used by default, so the following commands are equal:
kill -15 22298 kill -SIGTERM 22298
If the SIGTERM signal (-15) isn't enough, as for example when the Apache sockets are open, we can use the signal SIGKILL (-9):
kill -9 22298 kill -SIGKILL 22298
This signal guarantees that the process will be terminated, interrupting any task in execution. If Apache has several running processes we can eliminate them all at once:
kill -9 22298 222300 222302 kill -15 22298 222300 222302
In Linux, we also have the killall command to kill processes by name, although this Unix command has another function. For example:
killall -15 httpd
This will close all instances of Apache's httpd process on Linux. However, in Unix, this command will terminate all processes, so it will be better not to get confused when using it.
Do you know any other way to kill processes on Linux or get into troubles using any of the commands above? Let me know in the comments.
You may also want to know: Commands To Download & Extract TAR Files in Linux