tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sh: killing a pipe head from the tail



On Tue, Dec 07, 2021 at 06:38:32PM +0100, Edgar Fuß wrote:
> So the context is a collectd exec plugin translating envstat output to collectd 
> PUTVALs. The structure is about
> 
> envstat -i 10 | while read ...
> ...
> printf "PUTVAL ... || exit 1
> ...
> done
> 
> After I realized what the problem was (SIGPIPE ignored), I thought what to do
> and the obvious solution (apart from patching collectd) is to trap - PIPE 
> before calling envstat. But since that would make envstat continue to run 
> until it next tries to output something, I was asking myself whether there 
> was a more elegant solution where I could explicitly kill envstat from within 
> the while loop instead.

In a similar situation I have written a script like this,

(
	envstat -i 10 & echo $! > envstat.pid
) | while read ...
	...
	... || kill $(cat envstat.pid) && exit 1
	...
done

Usually it's necessary to protect against the loop racing against the
creation of envstat.pid.  Seems like there should be a better way.
 
Dave

-- 
David Young
dyoung%pobox.com@localhost    Urbana, IL    (217) 721-9981


Home | Main Index | Thread Index | Old Index