LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-07-2010, 09:44 AM   #1
schaefer76
LQ Newbie
 
Registered: May 2010
Posts: 4

Rep: Reputation: 0
Bash Script to Continuously Ping Server and Send Email


I'm trying to write a script that will continuously ping a server and then send out an email when the server is down, and then when it is back up. Then, continuing with monitoring. I would like to not run this in cron, because I don't want to script to run with multiple instances.

For example,
Ping a server every minute.

-If successful, do nothing.
-If failed to ping, then send out email stating that server is down.
-Once ping is successful, then send out email stating that server is up.

I only want it to send an email once after a failure, so the end user isn't get an email every minute that it fails. Once it is successful, then send the email (one time), stating that the server is up. Then, continue to ping and if fails again, repeat the process.

Anyone have any ideas? I'm new to this site so not sure if I'm posting in the right location. Thanks.
 
Old 05-07-2010, 09:58 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,992

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
Quote:
Originally Posted by schaefer76 View Post
I'm trying to write a script that will continuously ping a server and then send out an email when the server is down, and then when it is back up. Then, continuing with monitoring. I would like to not run this in cron, because I don't want to script to run with multiple instances.

For example,
Ping a server every minute.

-If successful, do nothing.
-If failed to ping, then send out email stating that server is down.
-Once ping is successful, then send out email stating that server is up.

I only want it to send an email once after a failure, so the end user isn't get an email every minute that it fails. Once it is successful, then send the email (one time), stating that the server is up. Then, continue to ping and if fails again, repeat the process.

Anyone have any ideas? I'm new to this site so not sure if I'm posting in the right location. Thanks.
Yep, just loop through the ping check, and put a sleep statement in it, so it pauses, and if it fails, send the email and exit the script. That way, it won't start up until you fix the problem, and restart the script.

Another alternative is to send the email, then go into another subroutine, which keeps checking the failed address until it FINDS it, then sends an email saying it's back up, which will throw the program back into the first subroutine.....

Post what you've written, and we can help. But we're no going to write a script for you, though.
 
Old 05-07-2010, 10:07 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Generally LQ does not write custom scripts to order (but you might get lucky); we prefer to help you to help yourself which means you show us what you have done and tried and where you got stuck and what doubts you have ...

To do what you want you will need a command scripting language of which bash is the most common choice. Do you want to use bash or some other language? What is your level of expertise in the chosen language?

Where is the email being sent? Locally, or via the Internet? Does the computer you are doing this on have a way of sending such mails?

What do you want to use to send mail? mail and mailx are possibilities; there are others (pine? mutt? I'm not sure).
 
Old 05-07-2010, 10:30 AM   #4
schaefer76
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for you input. I know some of the basic scripting, but you gave me enough guidance to get me started. Catkin, I know how to send the emails out through the script (maybe not the right way), so I now how to do that part. I wouldn't be doing this script if I didn't have a customer insisting that he gets an email when his server goes down and when it comes back up. I will do some research and see if I can put together something and will post what I came up with. Chances are, it will be a complete failure, but I'm still learning.

TB0ne, thanks for the advice on what to do. I will see what I can come up with. I know that it will be a subroutine that I will want to use, so I will head in that direction.

Thanks for all of the help.
 
Old 05-07-2010, 10:54 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by schaefer76 View Post
I will do some research and see if I can put together something and will post what I came up with.
Glad you're happy with that. Hopefully it will be a good way to learn.

You may like to use a variable to control sending the mails, something along the lines of this pseudo-bash-code
Code:
last_ping_OK_flag=unknown
while true
do
    ping
    if ping OK then
       if last_ping_OK_flag is false then send email
       last_ping_OK_flag=true
    else
       if last_ping_OK_flag is true then send email
       last_ping_OK_flag=false
    fi
    sleep a while
done
If the network is iffy you might like to wait for more than one successive ping failure before emailing.
 
  


Reply

Tags
script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
send email to server to start shell script bic Red Hat 5 12-04-2009 10:06 AM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 01:11 AM
bash script in background - awk and send email noir911 Programming 4 03-22-2009 10:46 PM
bash script to send email when internet cap is reached aeby Linux - Server 7 08-22-2007 08:46 AM
Write a script to send an email from bash kpelczar Linux - Software 5 02-09-2005 05:19 PM


All times are GMT -5. The time now is 05:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration