1

I have a script from which I run a second script in a subshell. What's the soonest that can send a signal to the second script?

script1:

./script2 &
kill -SIGCONT $!

script2:

echo "~~ ENTRY"

trap 'SUSPEND=false' SIGCONT

SUSPEND=true
while $SUSPEND; do; sleep 1; done

echo "~~ EXIT"

This won't work, the terminal will just hang there in "suspended" mode. My guess is that because I call kill straight away after I run script2, the trap in script2 doesn't have time to get parsed, and thus nothing happens - race condition.

So what's the soonest I can send the signal to the child process - making sure that it gets trapped?

2
  • There's no way to predict. Theoretically, if the system is busy enough with high-priority processes, script2 might NEVER get to the trap command.
    – Barmar
    Aug 29, 2014 at 18:24
  • What you could do is have script2 do something like write into a file after it traps the signal. script1 could loop, checking for a modification of the file.
    – Barmar
    Aug 29, 2014 at 18:25

1 Answer 1

1

You can use a message to a named pipe as a signal that script2 is ready to handle the signal.

script1:

mkfifo /tmp/pipe
./script2 &
read nothing < /tmp/pipe
kill -CONT $!

script2:

echo "~~ ENTRY"

trap 'SUSPEND=false' SIGCONT
echo ready > /tmp/pipe

SUSPEND=true
while $SUSPEND; do; sleep 1; done

echo "~~ EXIT"

You must log in to answer this question.

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