Question

I want to run a command as soon as a certain text appears in a log file. How do I do that in Bash?

Was it helpful?

Solution

Use command

tail -f file.log | grep --line-buffered "my pattern" | while read line
do
  echo $line
done

The --line-buffered is the key here, otherwise the read will fail.

OTHER TIPS

Using only tail:

tail -f file.log | while read line; do if [[ $line == *text* ]]; then
    mycommand
fi; done

This should work even without GNU grep:

tail -f -n 0 logfile.out | nawk '/pattern/ {system("echo do something here")}'

edit: Added "-n 0" so that only new occurences of the text will be matched.

Also you might look at inotail, a replacement for tail -f which uses the inotify framework to wake up only when the file you're interested in has changed. The usual tail -f just sleeps for short periods of time between polling, which is an effective but not very efficient solution.

I like matli's answer. Bruno De Fraine's answer is also good in that it uses only shell ccommands, not other programs (like awk). It suffers from the problem that the entire line must match the magic string. It's not clear from the question that's part of the requirment.

I would modify it a tiny bit to deal with the "as soon as" clause in the original question

logfile_generator | tee logfile.out | nawk '/pattern/ {system("echo do something here")}'

where logfile_generator is the program that is generating the log file in the first place. This modification executes the "something" as soon as the magic string is located.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top