Article 5KSQ8 Help with Bash script - email new error messages from several log

Help with Bash script - email new error messages from several log

by
Linux Rookie
from LinuxQuestions.org on (#5KSQ8)
Hi,

This is completely new for me (linux and bash noob), but i got the task to write a script to monitor errors or anything that failed from several logs and for every log email separately its newly arrived errors/failures to a central administration emailaddress.

For example for the log of nginx webserver.
I got something like:

#!/bin/bash
SEARCH=GET
SOURCE='/var/log/nginx/access.log'

function GetNewLogs() {
tail -n0 -F $SOURCE | grep --line-buffered $SEARCH
}

GetNewLogs | mail -s "nginx errors hostname" someemail@domain.com

I save this as test.sh and run it in a terminal like ./test.sh

This shell script needs to be scheduled and running all the time even after a reboot.

How can i accomplish this? I had tried to let the function write to a file first and then cat the file to pass to the mail command but it just hangs in the prompt. I am creating the script in a PUTTY window connected to Linux machine.

Thanks for any help.latest?d=yIl2AUoC8zA latest?i=ukEyQvTKgYA:rEXZQrUCFMk:F7zBnMy latest?i=ukEyQvTKgYA:rEXZQrUCFMk:V_sGLiP latest?d=qj6IDK7rITs latest?i=ukEyQvTKgYA:rEXZQrUCFMk:gIN9vFwukEyQvTKgYA
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments