2

There are two scripts, the first in the background, the second in the foreground:

trap "ret=\$?; rm -f pipe1 pipe2; exit \$ret" EXIT INT TERM QUIT
my.bin > pipe1 < pipe2 &
my-term.sh pipe1 pipe2

my-term.sh interacts with the user via stdin/stdout (it's like a CLI terminal program):

while read -ep "prompt> " l; do echo $l; done

my.bin can be simulated with just exit 0 script.

How to terminate my-term.sh if my.bin exits for any reason?

What works:

trap "ret=\$?; rm -f pipe1 pipe2; exit \$ret" EXIT INT TERM QUIT
(my.bin > pipe1 < pipe2; kill -9 $$) &
my-term.sh pipe1 pipe2

But it probably stops my-term.sh from cleanups. How to do it correctly?

0

Try a list of signals, if one fails, wait three seconds, (sleep time may be tweaked based on whatever my-term.sh is supposed to be doing), then try another:

(my.bin > pipe1 < pipe2; x=$$ ; \
 for sig in SIGTERM SIGINT SIGQUIT SIGABRT SIGKILL ; \
 do kill -${sig} $x && break ; sleep 3s ; done ; ) &
my-term.sh pipe1 pipe2
  • As, usual, I've posted an unreproducible example. Fixed now. – Velkan Oct 14 '16 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.