Skip to content
Related Articles

Related Articles

Shell Scripting – How to send Signal to a Processes

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 11 Jul, 2022
View Discussion
Improve Article
Save Article

Prerequisites: Processes, Bash Scripting, Shell Function Library

Signals allow the operating system to communicate with programs(or processes). We will create an interactive bash script to send signals to a process and observe the status of the process. For demonstration, we will use a program named xlogo to which we will send signals from our bash script. It’s a program that displays the X Window System Logo.

Install xlogo if it is not present in your system

$ sudo apt-get install x11-apps

Creating Shell Script

The name of the script will be signals.sh.

$ touch signals.sh 

Make it an executable file.

$ chmod +x signals.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 "Do you want to continue?(y/n)"
  read CHOICE
  if [[ $CHOICE == "n" ]]
  then
    # send SIGHUP(hangup signal to process)
    kill -1 $PID
    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 &
    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

It takes the name of a valid process as an argument in the terminal. The input is the signal number. Signals are sent via kill command and the jobs -l print the status of the process. While running the script if we terminate the process, there is an option to restart the process.

Run the Script

$ ./signals.sh xlogo

Start the script with xlogo as the argument. xlogo process starts running in the background.

You would see a small window pops up showing the X logo on your screen. By dragging it and resizing the window, the logo accordingly changes shape which means the program is running properly. The program is running in the background.

Press y to continue or n to exit the script.

Sending SIGSTP signal to process. The corresponding signal number is 19

Signal number 19 corresponds to a signal called SIGSTOP. It kind of pauses or freezes a program. It is used when we want to stop a process but do not want to close it.  The shell prompt returns. The terminal shows the current status of the process.

We can see that the X logo stops resizing when the window is dragged.

xlogo stops working. The logo fails to change size. The process is stopped/paused.

Sending SIGSTOP(19), SIGCONT(18), SIGINT(2) and SIGHUP(1) signals.

Sending SIGSTOP, SIGCONT, SIGINT and at last SIGHUP signal to the process

List of common signals

 Number               

   Name                           

Meaning

1SIGHUP         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.
2SIGINTIt 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.
9SIGKILLUnlike 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.
15SIGTERM                This is the default signal of the kill command
18SIGCONTThis will restore a process that was paused by a SIGSTOP or SIGTSTP signal
19SIGSTOPThis signal pauses/freezes the process. The process cannot choose to ignore it.
20SIGTSTPIt 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.
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!