Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 259
est Avatar asked Dec 02 '10 03:12

est


People also ask

How do I monitor log files in Linux?

Like any other OS, you can use certain commands to see Linux log files. Linux logs will display with the command cd/var/log. Then, you can type ls to see the logs stored under this directory. One of the most important logs to view is the syslog, which logs everything but auth-related messages.


1 Answers

tail -fn0 logfile | \ while read line ; do         echo "$line" | grep "pattern"         if [ $? = 0 ]         then                 ... do something ...         fi done 
like image 71
Wesley Rice Avatar answered Sep 21 '22 21:09

Wesley Rice