When any signal is received from os, at that time we need to perform cleanup or to close already open file descriptor and then exit the program/script.

Here I have given one example for SIGHUP using trap .

When process ID will receive kill -1 <PID> ,function1 will be called and while loop will be broken.

function1() {
echo “In function1”
echo “resetting flag”
FLAG=0
}

trap function1 1
FLAG=1
while [ $FLAG -eq 1 ]
do
echo “In while”
sleep 1
done

Advertisements