Open In App

Shell Scripting – How to send Signal to a Processes

Improve
Improve
Like Article
Like
Save
Share
Report

Signals allow the operating system to communicate with programs(or processes). There are about 30 standard signals implemented by the Linux OS.

We can send signals to processes via certain keyword strokes or by the kill or wait command.

Prerequisites: Processes, Bash Scripting, Shell Function Library

Sending signals to foreground processes

Go to the terminal and start a process, say xlogo in foreground. Install xlogo if it is not present in your system.

$ sudo apt-get install x11-apps
$ xlogo

After running the process in foreground, the shell prompt will be unavailable.

  • Pressing Ctrl+C sends an Interrupt signal (SIGINT) to the process and the process terminates. Restart it.
  • Pressing Ctrl+Z sends a STOP signal (SIGTSTP) to the process which kind of freezes/pauses the process and the shell prompt returns.
  • After pressing Ctrl+Z, you can unfreeze the paused process by moving it to foreground via fg %jobspec command or to the background using bg %jobspec command.

Ctrl+C terminates. Ctrl+z pauses/stops. bg command continues the process in background

Sending signals to background processes

$ xlogo &

Send signals to the process via the kill command. You need the jobspec or PID (process ID) of the xlogo process running in background.

You can view the jobspec or PID using the job or ps command.

$ kill -SIGTSTP PID
$ kill -SIGCONT PID
$ kill -SIGINT PID

Sending SIGTSTP, SIGCONT and SIGINT signal to xlogo process having PID 25648

  • SIGTSTP signal pauses the process
  • SIGCONT signal continues the paused process
  • SIGINT signal terminates the process.

Creating Shell Script

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.

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

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.

Last Updated : 20 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads