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?

1 Answer 1

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
2
  • As, usual, I've posted an unreproducible example. Fixed now.
    – Velkan
    Oct 14, 2016 at 6:52
  • This won't work. $$ is the PID of the current shell script, not the PID of its child my-term.sh.
    – vinc17
    Apr 19 at 16:11

You must log in to answer this question.

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