Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tail and grep log and mail (linux)

i want to tail log file with grep and sent it via mail like:

tail -f /var/log/foo.log | grep error | mail -s subject [email protected]

how can i do this?

like image 227
askaquestion Avatar asked Jan 11 '11 11:01

askaquestion


1 Answers

You want to send an email when emailing errors occur? That might fail ;)

You can however try something like this:

tail -f $log |
grep --line-buffered error |
while read line
do
    echo "$line" | mail -s subject "$email"
done

Which for every line in the grep output sends an email.

Run above shell script with

nohup ./monitor.sh &

so it will keep running in the background.

like image 105
Marcus Borkenhagen Avatar answered Oct 02 '22 07:10

Marcus Borkenhagen