I am running a simulation like this
./waf --run scratch/myfile | awk -f filter.awk
How can I kill the waf command as soon as filter.awk detects that something happened (e.g. after a specific line is read)?
I cannot change waf or myfile. I can only change filter.awk, and the above command (obviously).
Update after comments:
wafdoes not terminated after receivingSIGPIPE(as it should?)- It spawns child processes, that need cleaning up.
This is my own answer (and challenge).
After working on @thatotherguy's ans @Chris's answers, I simplified a bit and got this:
tmp=$(mktemp)
{ ./waf --run scratch/myfile & echo $! > "$tmp"; } | { awk -f filter.awk; pkill -P $(<$tmp); kill $(<$tmp); }
Unfortunately I could not get rid of the tmp file, every attempt to pass the PID as a variable failed.
I won't change the accepted answer (since it was the one that worked when it was really needed), but +1 for anyone that can simplify more.