Whether we are novice or advanced users, sooner or later we will find some rebel program or process that we will have to end by force. In Windows we are used to using the task manager to finish processes. Today we are going to focus on
how to kill a process in Linux
, for which we will use the console.
The terminal is a versatile tool in Gnu / Linux environments and we should not call it complicated, we just need a little practice.
Killing a process manually
is something very common and does not have to be dangerous if we do it correctly. Although we must bear in mind that if we kill processes on which the proper functioning of the operating system depends, this may have unexpected consequences or cause a critical failure.
Knowing the processes and how they identify with the PID.
A process in Linux is nothing more than a program that is running and has been assigned an identifier known as PID.
The PID is an abbreviation for
Process ID
, which refers to the process identifier.
Each process will have a unique PID.
To
obtain a detailed list of all processes
, we can execute the
ps aux
command in the terminal. Within the list of processes, we will see that one of the columns corresponds to the PID, which is an integer numerical value.
I will not explain in detail the meaning of each column, but I will remind you that you can link the previous command with
grep
to quickly find some process. For example, we can use
ps aux | grep apache
ps aux | grep apache
to locate the information of the Apache server process.
Parent processes and child processes in Linux.
We must also keep in mind that
processes can execute other processes
, called child processes. The child processes have their own PID, but at the same time they are assigned the
PPID
identifier
(Parent Process Identifier)
, which indicates the PID of the parent process.
Taking into account everything said about parent processes and child processes, we must also know the
ps -ef
command that shows us the PPID values.
Commands and tools to kill processes in Linux.
Let's start talking about the kill command, which is usually the most common when stopping processes.
The
kill
command can take as an argument the signal that will be sent to the process to kill, stop, resume or suspend it. The names of these signals with their corresponding numerical value are as follows:
-
SIGINT - 2
: This signal interrupts a process, equivalent to pressing the key combination
Ctrl + C
-
SIGKILL - 9
: Kills a process permanently
(cannot be recovered)
.
-
SIGTERM - 15
: interrupts a process just like SIGINT, but this signal can be interpreted by the process. When we use the kill command without specifying any signal, this is the one used by default.
-
SIGCOUNT - 18
: Resumes a process that has been stopped.
-
SIGSTOP - 19
: stops a process, it is equivalent to pressing the key combination
Ctrl + Z
The above are the most common signals, but you can check the complete list with the
kill -l
command.
If what we want is to kill a process immediately, the command would be
kill -9 PID
or its equivalent
kill -SIGKILL PID
. Use the one that is easier for you to remember.
Kill processes by knowing their name with the pkill and killall commands.
If we know the name of the process
instead of its PID, we can use the
pkill
command or the
killall
command. These two commands are similar, but not the same.
Following the previous example, we could
kill a process with pkill
as follows:
pkill -9 nombreproceso
.
Similarly, it is applicable for the
killall
command with the following example:
killall -9 nombreproceso
.
Here we can ask the question of
what happens when there are several processes that have the same name and one of these commands is executed
. The answer is simple, this command acts on all processes that have the specified name.
Differences between pkill and killall.
The pkill command uses a pattern to find the process name match, while killall searches for the exact match.
To understand this better, if we have a process whose name is
apache2
and we use the
pkill -9 apache
command, we will kill the apache2 process. If we use the
killall -9 apache
command, the apache2 process will not stop (because killall needs the exact name of the process).
Other tools, references and final words.
I hope this tutorial has helped you and now you know a little more closely the possibilities offered by the console in Linux.
I must remind you that in the blog we talked earlier about the
fkill-cli
tool, which allows you to kill processes interactively. There is another very similar tool but written with the
Go language
, whose name is
Gkill
.
I would also like to take this opportunity to refer to a forum article that talks about how to
stop stopped jobs
(zombie processes in the background)
.