Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 2 servers. one of them often crashes. I want to send a SMS to my phone when it crashes.

I wrote this simple bash script in another server and put it in crontab to run every 10 minutes.

wget --spider http://mywebsite.com || php sendsms.php

Is this the right way? sometimes I get SMS, but I see that server is running without any problem.

share|improve this question
    
Is the --spider really necessary? If the site is large, it could take a long time, and if the first page works, you already know Apache is working. Are there particular pages (perhaps CGI scripts) which may malfunction for other reasons? Then maybe use (one of) those as your test case. –  tripleee Jun 23 '13 at 6:22
add comment

1 Answer

You should be able to see from the output of wget --spider ... why wget has failed. That information is emailed to the user running the script or, if you use

MAILTO=your.email@address.com

you can redirect it to your email account for review.

If there are spurious network or DNS errors you can do a double check to see if unavailability was temporary, and thereby reducing the number of false positives:

wget --spider -a /var/tmp/wget_test.log http://mywebsite.com || (sleep 10; wget --spider http://mywebsite.com ) || php sendsms.php

Note that I have made the first try, append output to a log file (instead of printing to stderr). This way you don't get an email every 10 minutes if things run fine.

Every wget run first prints a date-timestamp and url when it tries that url. You will be able to review all runs that fail the first wget and pass the second by matching the date-timestamp from the emailed output of the second wget to that from the log file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.