Вопрос

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