I want to run a command as soon as a certain text appears in a log file. How do I do that in Bash?
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.
Using only tail
:
tail -f file.log | while read line; do if [[ $line == *text* ]]; then
mycommand
fi; done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With