echo "Started: $(date)"
trap "echo TRAPDATE: $(date)" SIGTERM
echo "PID: $$"
while true #((count<20))
do
sleep 2
(( count++ ))
echo $count : $(date)
done
exit 0
What this code actually does is that it prints the date and time every 2 seconds. When a SIGTERM(15) signal is send to the script it should trap the signal and print the corresponding time when the signal was issued.
I tried running the script by
./script.sh
everything works fine, until I send the SIGTERM signal
kill -15 108030
here's the sample output for your reference:
Started: Wednesday 05 October 2022 07:29:51 PM IST
PID: 108030
1 : Wednesday 05 October 2022 07:29:53 PM IST
2 : Wednesday 05 October 2022 07:29:55 PM IST
3 : Wednesday 05 October 2022 07:29:57 PM IST
TRAPDATE: Wednesday 05 October 2022 07:29:51 PM IST
4 : Wednesday 05 October 2022 07:29:59 PM IST
TRAPDATE: Wednesday 05 October 2022 07:29:51 PM IST
5 : Wednesday 05 October 2022 07:30:01 PM IST
6 : Wednesday 05 October 2022 07:30:03 PM IST
7 : Wednesday 05 October 2022 07:30:05 PM IST
8 : Wednesday 05 October 2022 07:30:07 PM IST
9 : Wednesday 05 October 2022 07:30:09 PM IST
TRAPDATE: Wednesday 05 October 2022 07:29:51 PM IST
10 : Wednesday 05 October 2022 07:30:11 PM IST
11 : Wednesday 05 October 2022 07:30:13 PM IST
12 : Wednesday 05 October 2022 07:30:15 PM IST
^C
you see the issue? The date that is being print from trap shows the running time of the script. I'm just a newbie trying to explore deep into linux and it'll be much helpful if someone could explain why this happens here?. Is the trap loaded into the memory and uses the same date each time it is called?