I'm running a command on command line which infinitely generates a certain data and I'm looking for a particular bit of data, so I used grep to find it. As soon as I get the data, I want the command to kill itself. How do I achieve this?

NOTE1: AND'ing kill $$ with grep is not terminating the command.

NOTE2: I saw this question, which does not seem to work in my case.

2 Answers 2


Try this

loud_program | grep --max-count=NUM

then, according to my limited knowledge, loud_program receives SIGPIPE because it is writing to a disconnected end, which in turn could terminate loud_program. Try it with your program, not sure if this works for all programs.


You can pipe the results to a pager, like less. The less input buffer will prevent the command from generating too many data (i.e. it will block at some point until you go more downward), and once you quit the pager, the commands will terminate automatically due to a broken pipe. If for some reason the commands (of the pipeline) are still running because data are not generated, you can type Ctrl-C to terminate them.

  • the command i was executing was automated and hence i could not use less. fyi Sep 5, 2014 at 10:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.