Skip to content
Advertisement

Sending signals to a linux process and intercepting them

I have a following bash script:

#!/bin/bash
for i in {0..8}
do
  trap "echo received $i" $i
done
trap "echo 'receiving the SIGINT'; kill -9  $$" INT
for i in {10..64}
do
  trap "echo receiving the $i" $i
done

sleep 1h

If run it and from other terminal send the SIGINT to it, it does nothing. I am using kill -2 pid where pid is the pid of the running script.

If I hit CTRL+C (SIGINT) in terminal where the script is running it kills itself and writes the message. How come, it is not the same when sending the signal to it from a different terminal?

Advertisement

Answer

If I replace the sleep 1h with the

while true ; do
    sleep 1
done

It is working 🙂

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement