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. |
Quote:
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. |
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). |
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. |
Quote:
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 |
All times are GMT -5. The time now is 10:04 PM. |