5

I am pausing a terminal script with SIGSTOP, and I want it to print a message that it is being paused.

  • Since I can't trap the SIGSTOP, I've tried sending a SIGUSR1 before the SIGSTOP and trapping that, but the command I put in the trap ends up being executed after the script is resumed.
  • If I put a sleep between the SIGUSR1 and the SIGSTOP, the trap command gets executed, but the script then runs a bit before the SIGSTOP is received.

Is there a way I can externally tell my script to echo "I'm being paused", then immediately stop?

Edit: I'm using bash. My test script is just:

trap "echo Paused" SIGUSR1
trap "echo Restarted" SIGCONT

while : ; do
    echo `date`
    sleep 1
done

I call this like "./blah.sh".

My control script is:

script_id=`pidof -x blah.sh`
kill -SIGUSR1 $script_id
sleep 0.5
kill -SIGSTOP $script_id

sleep 2

script_id=`pidof -x blah.sh`
kill -SIGCONT $script_id
1
  • 2
    Please edit your question and add the code (ideally, just a minimal example that reproduces the issue) you are using so we can see how you trap what etc. That will help us help you. Also make sure to tell us i) the shell you are using, ii) how you run the script (script.sh or bash script.sh or whatever) and iii) how you send the signals (kill? Ctrl+Z? something else?)
    – terdon
    Commented Nov 7, 2022 at 15:40

1 Answer 1

9

you should use SIGTSTP instead of SIGSTOP. See here:

The SIGSTOP signal stops the process. It cannot be handled, ignored, or blocked.

The SIGTSTP signal is an interactive stop signal. Unlike SIGSTOP, this signal can be handled and ignored.

Here's an example:

paused() {
    echo PAUSED
    kill -SIGSTOP $$ # This signal cannot be handled. Here the script is actually paused.
    echo CONTINUED
}
trap 'paused' SIGTSTP

Example:

$ ./test_pause.sh & disown
[1] 18852
$ kill -TSTP 18852
PAUSED
$ kill -CONT 18852
CONTINUED

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .