Is there a cheap way to monitor a log file like tail -f log.txt
, then if something like [error]
appears, execute a command?
Thank you.
Is there a cheap way to monitor a log file like tail -f log.txt
, then if something like [error]
appears, execute a command?
Thank you.
tail -fn0 logfile | \
while read line ; do
echo "$line" | grep "pattern"
if [ $? = 0 ]
then
... do something ...
fi
done
grep -q
to set the return code without displaying the matching lines. –
Bawdyhouse I also found that you can use awk to monitor for pattern and perform some action when pattern is found:
tail -fn0 logfile | awk '/pattern/ { print | "command" }'
This will execute command when pattern is found in the log. Command can be any unix command including shell scripts or anything else.
An even more robust approach is monit. This tool can monitor very many things, but one of them is that it will easily tail one or more logs, match against regex and then trigger a script. This is particularly useful if you have a collection of log files to watch or more than one event to trigger.
Better and simple:
tail -f log.txt | egrep -m 1 "error"
echo "Found error, do sth."
...
© 2022 - 2024 — McMap. All rights reserved.
tail -fn0 logfile
seems to be expire since the HTTP server log file are quickly renamed and gzip'ed. The script is still monitor the old file-descriptor I think? Is there a way to auto update file inode or something after a period of time for thetail
command? – Mannerless