I made bash script which calls two processes linked with named pipe, like this:
mkfifo pipe
gnuplot <pipe &
PIPE=pipe apl $*
rm pipe
wait $!
Here apl
sends commands through pipe
to gnuplot
. When first finished second should finish too, then the script terminates.
(Note, question is not related neither apl
nor gnuplot
, you may imagine any other application instead.
The problem is that apl
may, or may not open pipe (if it does not need to plot any graph). In case it not opening pipe gnuplot is waiting forever, and thus, script is not terminated.
If I try to terminate gnuplot with echo 'exit' >pipe
it hangs in opposite case.
If I just remove wait
, gnuplot
will wait in background forever.
I cannot force apl
to open and close a pipe (it controlled by operator which may forget doing it).
So how should both script and background process terminated?