Generating email with output results for multiple background scripts running on KSH

StackOverflow https://stackoverflow.com/questions/18699503

  •  28-06-2022
  •  | 
  •  

Pregunta

A little background first: We have 9 JVM servers that are outputting log files that I am parsing constantly to find when an error occurs. The logs roll every 5-10 minutes, but the file name does not change. I was using SupperPutty to open 9 sessions and run the below script against each log file, but this was only affective when I was at work.

tail -f *filename* | nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="Bind value for HASCHILDREN = 0"

So I wanted to turn the above script into a background script that would email me when an error occured. I first had to KSH into a second shell to avoid stopping the scripts when I exited (even when I set them up as NOHUP). I tried setting up the script as below, but it doesn't email me when the error occurs.

tail -f *filename* | nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="Bind value for HASCHILDREN = 0" | mailx -s "Childrens flag has been detected" *email* &

Any help would be greatly appreciated.

¿Fue útil?

Solución

You could try to schedule a cron job in the crontab file to call a script every 5-10 minutes that checks for any changes in your logs and sends you an email if an error entry is found.

From the crontab documentation:

The crontab command invokes an editing session that allows you to create a crontab file. You create entries for each cron job in this file. Each entry must be in a form acceptable to the cron daemon. For information on creating entries, see The crontab File Entry Format.

Exanple:

To run the calendar command at 6:30 a.m. every Monday, Wednesday, and Friday, enter:

    30 6 * * 1,3,5 /usr/bin/calendar

Alternatively, you can create a crontab file by specifying the File parameter. If the file exists, it must be in the format the cron daemon expects.

Otros consejos

So I set up a cron job and told it to email my corporate email account, but it's dropping the outputs into my unix mailbox. Am I missing something?

MAILTO="email"

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="Bind value for HASCHILDREN = 0" /maximo/logs/file1 >/dev/null 2>&1

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="Bind value for HASCHILDREN = 0" /maximo/logs/file2 >/dev/null 2>&1

If you include this '&> /dev/null' to redirect your cron jobs you won't get any output and cron won't mail anything. I think is easier for you to redirect your cron jobs just like you first did:

    | mailx -s "Childrens flag has been detected" *email*

The correct syntax is MAILTO="user_name", I suggest that if you use the above then also change this to MAILTO="" to disable email reports from cron because you will be sending an email through mailx instead.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top