Reputation: 3888
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?
Upvotes: 1
Views: 747
Reputation: 12353
If I insert the following code before rm pipe
it works for me:
exec 3>pipe
exec 3>&-
.
Explaination: The first exec
-call creates a filedescriptor 3
directed to pipe
. The second exec
-call closes the filedescriptor 3
. In your case gnuplot will terminate because its stdin
was closed - whether apl opened it beforehand or not.
Source: How to avoid echo closing FIFO named pipes? - Funny behavior of Unix FIFOs
Upvotes: 2