3

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. Commented Oct 23, 2020 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). Commented Oct 23, 2020 at 17:05
  • @ctrl-alt-delor I would like to thank you for taking your precious time and returning to the questions I asked. Commented Oct 23, 2020 at 17:48

1 Answer 1

7

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.