Solved

Shell script that will alert

Posted on 2014-04-08
14
326 Views
Last Modified: 2014-04-08
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

0
Comment
Question by:k3vsmith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39986491
Don't know if that's the issue, but I always used grep with "" or ''.

grep -i "Completed Successfully"
0
 

Author Comment

by:k3vsmith
ID: 39986577
ok I can try that.
Do I need to add in any verbage that if it finds neither to do nothing?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39986592
Nope.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:k3vsmith
ID: 39986597
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
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39986605
Yes.

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

I mean
tail -6 your.log | grep -i "Completed Successfully"
0
 

Author Comment

by:k3vsmith
ID: 39986608
That shows me the line where it says Completed Successfully
0
 

Author Comment

by:k3vsmith
ID: 39986612
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
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39986613
Then that command was a success, and $? is 0.

You're sending an email only when $? is not 0
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39986617
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
 

Author Comment

by:k3vsmith
ID: 39986625
Oh opps! Haha good catch. That worked. Thanks for your help!
0
 

Author Comment

by:k3vsmith
ID: 39986627
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
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39986641
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
 

Author Comment

by:k3vsmith
ID: 39986645
Oh ok I see what your saying. Let me try that...
0
 

Author Comment

by:k3vsmith
ID: 39986649
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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
winscp 000webhost.com 6 95
What is linux 4.8.22? Is it the same as ubuntu 16.10 ?? 2 70
list of sudo access for date range 5 34
sed/awk/tail: how to read 3'de last line 4 39
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question