Kill the previous command in a pipeline

坚强是说给别人听的谎言 提交于 2019-12-04 12:35:35

What makes this tricky is that waf misbehaves by not exiting when the pipe breaks, and it spawns off a second process that we also have to get rid off:

tmp=$(mktemp)
cat <(./waf --run scratch/myfile & echo $! > "$tmp"; wait) | awk -f filter.awk; 
pkill -P $(<$tmp)
kill $(<$tmp)
  • We use <(process substitution) to run waf in the background and write its pid to a temp file.
  • We use cat as an intermediary to relay data from this process to awk, since cat will exit properly when the pipe is broken, allowing the pipeline to finish.
  • When the pipeline's done, we kill all processes that waf has spawned (by Parent PID)
  • Finally we kill waf itself.

Use awk's exit statement. waf should exit as soon as the pipe connecting it to awk closes.

When awk exits, waf gets a SIGPIPE the next time it tries to write output, which should cause it to exit unless whoever wrote waf deliberately set it up to ignore SIGPIPEs telling it to exit, in which case you'll have to kill it manually with kill or some such. Something like:

./waf --run scratch/myfile | ( awk -f filter.awk; killall waf )

You might need a -KILL option to killall if waf is ignoring all exit signals.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!