Skip to content

Instantly share code, notes, and snippets.

@rav94
Last active December 16, 2017 04:12
Show Gist options
  • Save rav94/b5dc5c76e3254f660fa1cd4e2bd397a6 to your computer and use it in GitHub Desktop.
Save rav94/b5dc5c76e3254f660fa1cd4e2bd397a6 to your computer and use it in GitHub Desktop.
Shell script for reporting nohup.out general errors into specified email/s.
#!/bin/bash
#Check whether a temporary file exisits
if [ ! -e nohup_errors_log ];
#If not you are here
then
#Grep for the desired pattern within your log file to catch the Error and Save the content to a new temporary file
grep "\[[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\},[0-9]\{3\}\] ERROR" /home/ubuntu/ravindu/nohup.out | sort > nohup_errors_log
#Mail the recorded error list to the desired mail (You can mail it to multiple uses also)
mail -s "Error Log for nohup.out" [email protected] < nohup_errors_log
else
#Compare the difference between the nohup.out file and the content within the temporry file and only mail send me a mail only when ther s an error
comm -23 <(grep "\[[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\},[0-9]\{3\}\] ERROR" /home/ubuntu/ravindu/nohup.out | sort) nohup_errors_log | mail --exec 'set nonullbody' -s "Error Log for nohup.out" [email protected]
#Add the new content into our temporary file
grep "\[[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\},[0-9]\{3\}\] ERROR" /home/ubuntu/ravindu/nohup.out | sort > nohup_errors_log
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment