I have a server that I can use PHP on and a router that can be pinged from the Internet. I want to write a PHP script that sends a ping to the router every 5 minutes with the following results:
- If the ping succeeds, then nothing would happen.
- If the ping fails, then it waits a few minutes, and if it still fails it sends a warning to my e-mail address once.
- After the router is pingable again it sends an e-mail that it’s OK.
Could this be done with PHP? How? Does anybody has a small PHP file that does this?
Below I’ve written a simple PHP script that does what you ask. It pings a server, logs the result to a text file (“up” or “down”), and sends an email depending whether the previous result was up or down.
To get it to run every five minutes, you’d need to configure a cron job to call the PHP script every five minutes. (Many shared web hosts allow you to set up cron jobs; consult your hosting provider’s documentation to find out how.)