1

I have a bash script sleep_loop.sh:

#!/bin/bash
for a in $(seq 1 100)
do
    sleep 1000
done
echo after_loop

Now I run this script using the command bash sleep_loop.sh. Then I open another terminal, get the PID of the sleep process and store it in pid_of_sleep, and run kill -SIGTSTP $pid_of_sleep. The result is that the job bash sleep_loop.sh is suspended in the original shell (which can be resumed using fg), as expected.

However, if I run the script using bash -i sleep_loop.sh so that the bash executing the script is in interactive mode, and repeat the above steps to send SIGTSTP to the sleep process, then the original terminal echos after_loop before the whole script is terminated. So it seems that after sleep exits from SIGTSTP, the bash running the script breaks out of the loop.

Here are my questions:

(1) What causes bash to behave differently in interactive and non-interactive mode after sleep receives SIGTSTP?

(2) In interactive mode, why does bash just breaks out of the loop without terminating the whole script after sleep receives SIGTSTP, even if the script does not contain any break statement?

Qin Yixiao
  • 113
  • 7

0 Answers0