Say I have a server writing stdout to a consumer

my server |  upon 'some pattern' --exit=0

so basically when the consumer reads stdout, and sees a certain pattern it will exit. my question is - when it exits, how I clean up the pipeline and potentially make the server a background process?

Maybe this is just a better way to do it:

( my server &> $file ) &

while true; do
  cat $file | upon 'some pattern' --exit=0

basically instead of sleep 5 seconds or what not, I am hoping to use a more evented approach, that guarantees the server is ready.

  • What do you mean by "clean up the pipe"? If you want to redirect the output of server to /dev/null when a pattern is seen by the right hand of the pipeline, I don't think that's possible, in a clean way. You can start the server in background and have the right hand transform itself in a lightweight cat upon seeing the pattern. Eg: { server & } | { sed /pattern/q; cat >/dev/null & }. And there are hacks like this to force redirect file descriptors from a running process. – mosvy May 18 at 17:37
  • well I'd rather avoid using the temp file and the while loop...i assume exit breaks out of the loop but doesnt stop the outer script – user282164 May 18 at 19:07
  • your temp file / poll (actually busy loop poll) approach is worse IMHO than the extra cat solution from my comment. – mosvy May 18 at 19:10
  • another approach is server > tmpfile & tail -f tmpfile | grep -m1 pattern but then the server will keep writing to tmpfile and filling up your disk. – mosvy May 18 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy