- Hi and welcome to the next video of the last section.
Killing Processes and Send or Respond to Signals.
In the previous video, we have seen how to gather
information about processes.
This video is an introduction to signals and their usage.
Termination of processes is an important task
we always come across including the need
to terminate all instances of program.
The command line provides several options
for terminating programs.
An important concept regarding processes in
Unix-like environments is that of signals.
Signals are an inter-process communication mechanism
used to interrupt a running process to perform some action.
Termination of a program is also performed by using
the same technique.
Signals are an inter-process mechanism available in Linnox.
We can interrupt a process using a specific signal,
which is associated with an integer value.
When a process receives a signal, it responds
by executes a signal handler.
It is possible to send and receive signals
and respond according to the signals
in shell script as well.
Kill is a signal used to terminate a process.
The event, such as control plus C
and control plus Z also send two types of signals.
The kill command is used to send signals to processes
and the trap command is used to handle
the received signals.
In order to list all the signals available, use...
It will print signal numbers and corresponding signal names.
Terminate a process as follows...
The kill command issues a term signal by default.
The Process ID List is to be specified
with a space as a delimiter between process IDs.
In order to specify a signal to be sent to a process
via the kill command, use...
The signal argument is either a signal name
or a signal number.
Though there are many signals specified for different
purposes, we frequently only use a few signals.
They're as follows, SIGHUP 1.
Hang up protection on death of the controlling process
or terminal.
SIGINT 2, signal which is omitted when control C is pressed.
SIGKILL 9, used to force kill a process.
SIGTERM 15, used to terminate a process by default.
SIGTSTP 20, signal emitted when control Z is pressed.
We frequently use force kill for processes.
In order to force kill a process, use...
Let's walk through the additional commands used
for terminating and signaling processes.
The kill family of commands.
The kill commands takes the process ID as the argument.
There are also a few other commands in the kill family.
Except the command name is the argument
and send a signal to the process.
The killall command terminates
the process name as follows...
In order to send a signal to a process by name, use...
In order to force kill a process by name, use...
Specify the process by name, which is specified
by users who own it by using...
If you want killall to interact to be confirmed
for killing processes, use the minus I argument.
The pkill command is similar to the kill command,
but by default, accept a process name
instead of a process ID.
For example...
Signal is the signal number.
The signal name is not supported with pkill.
It provides many of the same options
that the kill command does.
Check the pkill man pages for more details.
Capturing and responding to signals.
Trap is a command used to assign signal handler
to signals in a script.
Once a function is assigned to a signal
using a trap command, while the script runs
and it receives a signal, this function is executed
upon reception of a corresponding signal.
The syntax is as follows...
SIGNAL LIST is delimited by space.
It can be signal number or signal name.
Let's write a shell script that responds
to the SIGNT signal.
Run this script in a terminal.
When the script is running, if you press control and C,
it will show the message by executing
the signal handler associated with it.
Control plus C corresponds to a SIGNT signal.
The while loop is used to keep processes
running in an infinite loop without getting terminated.
This is done so they can respond to the signals
that are sent to the process asynchronously
by another process.
The loop that is used to keep the process alive infinitely
is often called the event loop.
We can send a signal to the script
by using the kill command and the process ID of the script.
Process ID of the proceeding script
will print it when it's executed
or you can find it out by using the PS command.
If no signal handlers are specified for signals,
it will call the default signal handlers
assigned by the operating system.
Generally, pressing control and C
will terminate a program as a default handler
provided by the operating system will terminate the process.
But, the custom handler defined here
specifies a custom action upon receipt of the signal.
We can define signal handlers for any signals available.
Kill minus L, by using the trap command.
It's also possible to set single handlers
for multiple signals.
Awesome.
We've successfully learned about killing processes
and send or respond to signals.
In the next video, we'll learn how
to send messages to User Terminals.