I created this script:
code fn()
{
sleep 1001
}
fn &
sleep 1000
echo "finish"
When I runt it and list processes (it's pid is 11570):
$ ps xao pid,ppid,pgid,command | grep 11570 |grep -v grep
11570 24322 11570 /bin/bash ./bash.sh
11577 11570 11570 /bin/bash ./bash.sh
11578 11570 11570 sleep 1000
11579 11577 11570 sleep 1001
then I'm killing it with ctrl+c and same ps returns:
11577 1 11570 /bin/bash ./bash.sh
11579 11577 11570 sleep 1001
Then:
kill -INT 11579
And nothing happnes (still same processes in bg)
I know that ctrl+c is same as killing it by:
kill -INT -11570
The same happens (same two processes are still in background). Then - in next run - I killed processes within group one by one and found that the only one which cares is sleep which is not in function.
I thought then that main process finishes then and that's why it finishes. But then 'finish' should be printed. (as it is when I interrupt just this sleep)
And now the questions are:
Why is only one sleep answering to interruption signal?
How to make this script cleanup processes in backgound on interruption?