I have a pipeline where a long-running command feeds into a filter. If the filter finds "foo", I want the long-running command to die.
In general this is not possible, because sibling processes (two children of the same parent) do not have any knowledge of each other. But consider the following example and answers:
We'll assume the following pipeline, which uses Linux's inotifywait program:
In this example, the user wants to stop the inotifywait program if a rename is performed with the resulting filename containing the string "abort". What are our options here?
Simply exit from the while loop, and do not explicitly try to do anything to inotifywait. The shell process containing the while loop will terminate, and the inotifywait process will continue running. As soon as inotifywait tries to write another line of output to the pipe (which has been closed), it will receive a SIGPIPE signal, which will terminate it.
Find an option within the feeder process to exit when something bad happens, instead of relying on an external filter to do it for you. (This will only be possible with certain feeder programs, but it's worth taking the time to read the documentation and see whether it's possible.)
Replace the anonymous pipeline with a FIFO and run the feeder as a background process explicitly. Save its PID. Wait for the filter to exit, and when it does so, kill the long-running feeder process. Thus: