1

For example, while I am running the script below, if I send an INT signal with Ctrl + C, the script file gets interrupted immediately. But when I try the same process with the kill command as kill -2 pid, the sleep command is expected to finish for the interrupt signal to be valid. What exactly is the reason for this situation?

#!/bin/bash

trap 'echo signal received!!' SIGINT

echo "The script pid is $$"
sleep 30
3
  • @ctrl-alt-delor You are right, i fixed now.
    – testter
    Oct 23 '20 at 16:59
  • 1
    What happens if you send sigint to the script and to sleep? (My guess is that this is what ctrl-c is doing). Oct 23 '20 at 17:05
  • @ctrl-alt-delor I would like to thank you for taking your precious time and returning to the questions I asked.
    – testter
    Oct 23 '20 at 17:48
3

The difference is that when you press Ctrl-C the kernel will send a SIGINT signal to both your script and the sleep command (i.e. it will send the signal to the whole process group), but with kill -INT $pid you're only signaling $pid (supposedly your script).

Assuming that your script is started from a typical interactive shell (i.e. the script is the process group leader), just negating the pid should work: kill -INT -$pid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.