Shell Scripting – Shell Signals Values
Prerequisites: Processes, Bash Scripting, Shell Function Library
Signals help the operating system to communicate with processes and vice-versa. Signals are also generated when processes don’t function properly or try to access prohibited memory. Different signals are mapped to different numbers which are referred to as signal values. The Linux OS implements about 30 standard signals.
We can send signals to particular processes by commands like kill and wait. The syntax generally is
$ kill -signalNumber processID(or %JOBSPEC)
$ kill -signalName processID(or %JOBSPEC)
Here is a list of the most commonly used signals and their meanings :
Number
|
Name
|
Meaning
|
1
|
SIGHUP
|
If a process is being run from a terminal and that terminal itself is closed/terminated then the process receives this signal and consequently terminates. |
2
|
SIGINT
|
It politely tells the program to terminate. Performs the same function as Ctrl+C. It’s up to the process whether it will listen to it or not. |
9
|
SIGKILL
|
Unlike other signals, the SIGKILL signal is never sent to the process. Instead, the terminal immediately kills the program and the program doesn’t get the time to save its data or clean up its work. Only use this as a last resort. |
15
|
SIGTERM
|
This is the default signal of the kill command |
18
|
SIGCONT
|
This will restore a process that was paused by a SIGSTOP or SIGTSTP signal |
19
|
SIGSTOP
|
This signal pauses/freezes the process. The process cannot choose to ignore it. |
20
|
SIGTSTP
|
It is similar to SIGSTOP but the process receiving this signal is under no obligation to listen to it. The process may choose to ignore it. |
List of other Shell Signals
Number
|
Name
|
Meaning
|
4 |
SIGILL |
Illegal instruction. The program contained some machine code in the CPU can’t understand |
5 |
SIGTRAP |
This signal is used mainly from within debuggers and program tracers. |
6 |
SIGABRT |
The program is called the abort() function. This is an emergency stop |
7 |
SIGBUS |
An attempt was made to access memory incorrectly. This can be caused by alignment errors in memory access etc. |
8 |
SIGFPE |
A floating point exception happened in the program. |
10 |
SIGUSR1 |
Left for the programmers to do whatever they want. |
11 |
SIGSEGV |
An attempt was made to access memory not allocated to the process. This is often caused by reading off the end of arrays etc |
12 |
SIGUSR2 |
Left for the programmers to do whatever they want. |
13 |
SIGPIPE |
If a process is producing output that is being fed into another process that consumes it via a pipe (“producer | consumer”) and the consumer dies then the producer is sent this signal. |
14 |
SIGALRM |
A process can request a “wake up call” from the operating system at some time in the future by calling the alarm() function. When that time comes round the wake up call consists of this signal. |
17 |
SIGCHLD |
The process had previously created one or more child processes with the fork() function. One or more of these processes has since died. |
21 |
SIGTTIN |
The operating system sends this signal to a backgrounded process when it tries to read input from its terminal. The typical response is to pause (as per SIGSTOP and SIFTSTP) and wait for the SIGCONT that arrives when the process is brought back to the foreground |
22 |
SIGTTOU |
The operating system sends this signal to a backgrounded process when it tries to write output to its terminal. The typical response is as per SIGTTIN. |
23 |
SIGURG |
The operating system sends this signal to a process using a network connection when “urgent” out-of-band data is sent to it. |
24 |
SIGXCPU |
The operating system sends this signal to a process that has exceeded its CPU limit. You can cancel any CPU limit with the shell command “ulimit -t unlimited” prior to running make though it is more likely that something has gone wrong if you reach the CPU limit in make |
25 |
SIGXFSZ |
The operating system sends this signal to a process that has tried to create a file above the file size limit. You can cancel any file size limit with the shell command “ulimit -f unlimited” prior to running make though it is more likely that something has gone wrong if you reach the file size limit in make. |
26 |
SIGVTALRM |
This is very similar to SIGALRM, but while SIGALRM is sent after a certain amount of real-time has passed, SIGVTALRM is sent after a certain amount of time has been spent running the process. |
27 |
SIGPROF |
This is also very similar to SIGALRM and SIGVTALRM, but while SIGALRM is sent after a certain amount of real-time has passed, SIGPROF is sent after a certain amount of time has been spent running the process and running system code on behalf of the process. |
28 |
SIGWINCH |
(Mostly unused these days.) A process used to be sent this signal when one of its windows was resized. |
29 |
SIGIO |
(Also known as SIGPOLL.) A process can arrange to have this signal sent to it when there is some input ready for it to process or an output channel has become ready for writing. |
30 |
SIGPWR |
A signal is sent to processes by a power management service to indicate that power has switched to a short-term emergency power supply. The process (especially long-running daemons) may care to shut down cleanlt before the emergency power fails |
31 |
SIGSYS |
Unused. |
To get the list of all the signals and their corresponding signal values, use the command kill -l.
For demonstration, we will use a program named Gedit to which we will send signals from our bash script and see the effect of different signal values on the program and how the program responds to it. Gedit is a GUI text editor for Linux.
Install gedit if it is not present in your system.
$ sudo apt-get install gedit
Creating Shell Script
The name of the script will be shellSignals.sh
$ touch shellSignals.sh
Make it an executable file.
$ chmod +x shellSignals.sh
Open the file and add the following Script.
#!/bin/bash
# enabling job control in the script
set -m
# starting the program in background
$1 &
echo "$1 process started in background..."
# storing the Process ID(PID)
PID=`pgrep $1`
while [[ true ]]
do
# print the job status
echo "$(jobs -l)"
# terminate the script
echo -n "Do you want to continue?(y/n) :"
read
if [[ $REPLY == "n" ]]
then
# send SIGTERM signal to the process
echo "Sending SIGTERM signal to the process."
kill -SIGTERM $PID
sleep 1;
echo "$(jobs -l)"
break
fi
# restart the process if it has been terminated
# checks whether PID exists or not. If not, it
# creates new instance of the process
if ! ps -p $PID > /dev/null ; then
echo -e "\nRestarting $1 process which was terminated earlier"
$1 &
sleep 1;
echo "$(jobs -l)"
PID=`pgrep $1`
fi
# sending signal to the process via kill command
echo -e "\nEnter Signal Number"
read NUM
kill -$NUM $PID;
sleep 1;
done
exit 0
The various shell signals can be sent via this shell script to any process. It is a user-interactive script that prompts whether to exit the script or not and prompts what shell signal values we want to send to the process. It also automatically restarts a process if we terminated it earlier.
Run the Script
$ ./shellSignals.sh gedit
Sending SIGSTOP(19), SIGCONT(18), SIGINT(2) and SIGTERM(15) signals.
Last Updated :
07 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...