In this lecture, I'll show you how to interact with processes. Sometimes you'll have applications 1
that become unresponsive or consume lots of resources and you want to stop them. 2
There are several commands that allow you to terminate the processes and kill is the most used one. 3
The kill command sends a signal to a process or a group of processes, causing them to act according 4
to the signal. 5
When the signal is not specified, it's default to 15 or SIGTERM. 6
To get a list of all available signals run the kill command with the -l option. Each signal 7
is identified by a number and a name. 8
For example, this signal is identified by number 15. 9
sig term or term. 10
But what are in fact these signals? A signal is an asynchronous notification sent to a process that determines 11
how the process should behave when the signal is delivered. 12
This is a list of all implemented signals. 13
For example, if a process gets the term signal, then that's an invitation for the process to enter 14
into the terminated state. 15
I've just said the word invitation on purpose; that's because the process can decide to ignore some of the signals. 16
Let's get started with some examples! 17
I'm opening an application like gedit which is a text editor. 18
From the terminal, I'll send the INT signal to the process and there are 3 ways to do that using the 19
kill command. Btw when you press Ctrl + C the foreground process running in the current 20
terminal will be delivered the INT signal which means INTERRUPT. The first step in sending a signal to 21
a process is to find the process id 22
so pgrep -l gedit 23
This is its pid; the first way to deliver the signal to a process is to use the number that identifies the signal, 24
in this case 2, which means SIGINT or INT. 25
So kill -2 and the process id 26
The editor will get terminated. 27
It's gone. 28
And the second example, I'm starting Firefox.. 29
Now, I want to find its pid. A quick way to find the pid of a process is by running the pid of command 30
So pidof firefox 31
And it printed out the PIDs of all firefox processes. 32
There are more! And the second way to deliver a signal to a process is to use the signal name. 33
So kill - 34
INT, it's the interrupt signal, 35
and the process id; I'll copy and paste the processes' ids. 36
Note that the signal name must be all in caps. 37
So if you want to provide more more pids to the kill command just specify the PIDs separated 38
by a space. 39
And it's gone! And the last way is to use SIG prefix and the signals name: 40
I'll do it again; I'm starting Firefox 41
It started! To avoid copying and pasting the pids you can use a command substitution like this: 42
so kill -SIGINT 43
$ a pair of parenthesis and the command (pidof firefox). This construction will replace 44
pidof command with its output which is the process . 45
IDs. I'm hitting enter and it's gone. We will dive deeper into this in the bash scripting section. 46
So all these kill commands are equivalent. 47
This one, this one or this one! Note that nonprivileged users can send signals only to their own processes 48
while the root user can send signals to other user's processes. 49
Another useful signal is HUP or number 1 which is used to reload a process. In the next example, 50
I’ll send the HUP signal to the ssh daemon to make it reload its configuration. In Linux a server reads its configuration 51
only at the start up. 52
So if you make any changes to the servers configuration, you have to reload it so that the new changes 53
come into effect. 54
The ssh demon is running and I'll check it using the systemctl 55
command. 56
sudo systemctl status sshd and it's active and running. If it's not running, try to start 57
it by running systemctl 58
start ssh. 59
Like this! 60
And if it's not installed, try to install it by running sudo apt install ssh 61
I'm opening another terminal and see the tail or the last lines of the ssh daemon log file in real time, 62
so tail -f and the path to the log file; it's /var/log/auth.log 63
The process id of a server is normally found in a file typically located in /var 64
/run directory. 65
The name of the file is the name of the daemon. 66
So this is the file where the precise id of the ssh daemon is saved. 67
This is its process id. 68
I've got the same result running pgrap. And I'm sending the hup signal to it, so sudo kill -1 69
and the process id. 70
Take a look at the other terminal. 71
You saw in the other terminal how the service was restarted. 72
By the way, you can also use a command substitution like I've already shown you. 73
It's the same! The last two commands are equivalent. 74
You should also know that a process can catch some signals and ignore them. 75
For example, when sending the term signal to a process, a few different things can happen. 76
The process may stop immediately. 77
The process may stop after a short delay, after cleaning up resources, or the process may keep running 78
indefinitely. 79
So it simply ignores the signal. The process can determine what it wants to do once the term signal is 80
received. 81
While most applications will clean up their resources and stop, some may not. That's why the term signal 82
is also called a soft kill! For some processes that ignore the term signal 83
it may be necessary to send the kill signal, which is number nine instead. 84
This one! 85
This signal cannot be caught. 86
This is also known as a hard kill. 87
If you want to stop an application that simply ignores the term signal, then you could try to send 88
the kill signal to it. 89
So kill-9 or -kill 90
and the process id. Besides the kill command, you can also send signals to processes using killall and 91
pkill. 92
The killall command kills all processes with a particular name. Maybe an application is running several 93
times and you want to kill all instances of that application. 94
I'm starting a few instances of sleep: sleep 95
123& I'm starting it in the background and another one: 96
By the way, sleep poses or delays the execution for a given amount of seconds. 97
There are two processes running! Now 98
I want to terminate all sleep instances. 99
Of course, I can also find all the processes ids and run kill, but it's easier to run the killall command. 100
It does 101
the same as the kill command just that it takes the name of the process instead of the process id. 102
It will then kill all the processes that share that name. 103
So kill-15. 104
This is the default anyway, and the name of the command sleep. 105
Of course, it's killall, not kill. 106
We are talking about killall. 107
And the processes were terminated. 108
Killall does not accept partial names; if you want to kill a process using a partial name, use pkill instead. 109
I'm starting a few instances of sleep again. 110
If I run the killall command using a partial name, it won't work/; killall slee It's not working but 111
I can run pkill providing a partial name so pkill slee and the proceses were killed. 112
At the end, I have a warning: kill, killall and pkill are extremely powerful 113
commands. If they are misused, they can bring a server down. 114
If you are new to these commands, it's recommended to learn very well how to use them on a non production 115
system.