Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I found this solution: http://stackoverflow.com/questions/7178888/grep-q-not-exiting-with-tail-f

but is there any other possible?

UPD: I want to do tail -f | grep -q something && echo 'found!' but it doesn't work because tail -f prevents pipe from being closed, so echo command don't have a chance to be executed.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Try at your own risk:

tail -n 0 -f /tmp/bar | { grep -q -m1 zoo && echo found ; pkill -P $$ '^tail$' ; }

The pkill command is necesary if the match is in the last line. But may kill other tail process in background if any from the same parent.

share|improve this answer
    
Thanks a lot. An addition of curly braces and pkill part is a true awesomness. –  user7477 Jun 1 '12 at 8:11
    
Is it possible to make pkill to kill only that previous tail instead of all tail commands that run in current script? –  user7477 Jun 1 '12 at 11:48

I believe you want to use grep's -q or -m options. If you want to actually see the matching line, use -m1, which will print the line and exit (sending a SIGPIPE to tail, causing it to exit). If you don't care to see the line, use -q, which will just exit when it finds the first occurrence.

share|improve this answer
    
" If you don't care to see the line, use -q, which will just exit when it finds the first occurrence." - that's the problem, -q doesn't exit, as you can see from my script sample. –  user7477 Jun 1 '12 at 7:31
    
Odd, it works for me. Regardless, the -m should work, just dump output to /dev/null –  Kevin Jun 1 '12 at 12:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.