Stop Tail when a certain string is found in log file

Viewed 609

Need to monitor a log file for a particular string "Server running at http". Once that string is found in the log file , I need to stop checking and want to continue to rest of the code.

currently I am currently using "tail -f my-file.log | grep -q "Server running at http" . But this doesn't seem to work.tail command is still running.

tail -f my-file.log | grep -q "Server running at http"
2 Answers

You can try something like :

tail -f my-file.log | awk '/Server running at http/ { print | bash file_with_code}'

P.S. instead of a different file it can be a function within the same script, needless to say that in such a case you will not need to say bash before function

Another possible solution can be:

tail -f my-file.log | egrep -m 1 "Server running at http";echo "found the pattern"

You have to specify, how many matches you need. As you need first hit

tail -f server.log | grep -m 1 "mystring"

once "mystring" found for the first hit, program will exit automatically.

Related