문제

On sh or bash, how do I exit the first process in a pipe when the second one has exited?

I was using the following to get input from the network:

$ nc -l 1234 | myprog

myprog exited due to an internal cause but nc continues to live. Is it possible to stop nc also?

도움이 되었습니까?

해결책

In bash you can fork process with & control operator and get the pid of child reading $!. So, the simplest solution could be

$ ( nc -l 1234 & echo $! > /tmp/myprog_kill_pid ) | myprog; kill $(</tmp/myprog_kill_pid); rm /tmp/myprog_kill_pid

Of course, this is not very nice and not suitable for multiple running instances... but you can start from this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top