Shell script that will alert

I have a script that runs every 10 minutes that looks at a log file. Its not working as I would like. Can you tell me what I'm doing wrong?

In Log:
You would see Completed successfully in which case I want script to shoot me a message.
If there is a failure you would see failed in log which I want it to shoot me a message.
If neither, please dont shoot me a message (process would still be running in which I dont need alert).

Currently I get a message saying completed successfully and when I check it hasnt completed. Im guessing I have it scripted in a way that it will shoot me that alert regardless? Please assist.

failmsg=`tail -6 $1 |grep -i FAILED`
if [[ $? = 0 ]];then
mail -s "Failed... please check..." #@vtext.com < /dev/null
fi

runmsg=`tail -6 $1 |grep -i Completed Successfully`
if [[ $? != 0 ]];then
mail -s "Completed.." #@vtext.com < /dev/null
fi

Open in new window

k3vsmithAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
Then that command was a success, and $? is 0.

You're sending an email only when $? is not 0
0
 
Dan CraciunIT ConsultantCommented:
Don't know if that's the issue, but I always used grep with "" or ''.

grep -i "Completed Successfully"
0
 
k3vsmithAuthor Commented:
ok I can try that.
Do I need to add in any verbage that if it finds neither to do nothing?
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
Dan CraciunIT ConsultantCommented:
Nope.
0
 
k3vsmithAuthor Commented:
That didnt send an alert.
For the line you suggested I change I changed like this:
runmsg=`tail -6 $1 |grep -i "Completed Successfully"`


Is that what you meant?
0
 
Dan CraciunIT ConsultantCommented:
Yes.

If you run that line directly, what's the output?

I mean
tail -6 your.log | grep -i "Completed Successfully"
0
 
k3vsmithAuthor Commented:
That shows me the line where it says Completed Successfully
0
 
k3vsmithAuthor Commented:
So does that mean in the script possibly get rid of the single quotes?
So:
runmsg=`tail -6 $1 |grep -i "Completed Successfully"`
to:
runmsg= tail -6 $1 |grep -i "Completed Successfully"
0
 
Dan CraciunIT ConsultantCommented:
Did you copy that script from somewhere? Cause it doesn't make much sense to assign a value to a variable (runmsg) and then forget about it...

The reason you were getting that email before was that because you were using an incorrect syntax for grep (unknown parameters because you had a space), it returned an error code.
0
 
k3vsmithAuthor Commented:
Oh opps! Haha good catch. That worked. Thanks for your help!
0
 
k3vsmithAuthor Commented:
No I just am still learning to script and was trying to hash something out rather than checking my log every so often when I run this process.
0
 
Dan CraciunIT ConsultantCommented:
I would test if that works even before the text "completed" is in the log. I would think it will send you an email regardless of if the text is present or no, just because an empty result is a valid result of grep so $? should always be 0.

I would try something like:
runmsg=`tail -6 $1 |grep -i "Completed Successfully"`
if [[ runmsg != '' ]];then
mail -s "Completed.." #@vtext.com < /dev/null
fi

Open in new window

0
 
k3vsmithAuthor Commented:
Oh ok I see what your saying. Let me try that...
0
 
k3vsmithAuthor Commented:
Ok so I kicked off process and there would be no fail or successfully in log and ran script. I got no alert. So it must be ok..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.