2

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 Answers2

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.

Sebastian
  • 8,817
  • 4
  • 40
  • 49
0

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.

vinc17
  • 12,174