Shellscript to monitor a log file if keyword triggers then execute a command?

est picture est · Dec 2, 2010 · Viewed 73.1k times · Source

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.

Answer

Wesley Rice picture Wesley Rice · Dec 2, 2010
tail -fn0 logfile | \
while read line ; do
        echo "$line" | grep "pattern"
        if [ $? = 0 ]
        then
                ... do something ...
        fi
done