使用 PHP 创建 ping 正常运行时间服务
我有一台可以使用 PHP 的服务器和一个可以从 Internet ping 通的路由器。我想编写一个 PHP 脚本,每 5 分钟向路由器发送一次 ping,结果如下:
- 如果 ping 成功,则不会发生任何事情。
- 如果 ping 失败,则会等待几分钟,如果仍然失败,则会向我的电子邮件地址发送一次警告。
- 路由器再次可 ping 通后,它会发送一封电子邮件表示一切正常。
这可以用 PHP 来完成吗?如何?有人有一个小 PHP 文件可以做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面我编写了一个简单的 PHP 脚本,可以满足您的要求。它对服务器执行 ping 操作,将结果记录到文本文件(“向上”或“向下”),并根据之前的结果是向上还是向下发送电子邮件。
要让它每五分钟运行一次,您需要配置一个 cron 作业以每五分钟调用一次 PHP 脚本。 (许多共享网络主机允许您设置 cron 作业;请参阅托管提供商的文档以了解如何操作。)
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.)
如果可以从互联网 ping 通并且在其上运行 HTTP 服务器,我个人会使用 Pingdom 服务。无需真正深入编写特殊脚本。
I personally use the Pingdom service if it can be pinged from the internet and is running an HTTP server on it. No need to really go deep into writing a special script.
据我所知,您无法使用 PHP 创建 cronjob ,但您可以做的是使用 crontab
和 这个 这样你就能够 ping 到所需的主机,也可以运行
exec("ping 1.2.3.4")
在您的脚本中
well as far as i know you can't create a cronjob with PHP , but what you can do is use crontab
and this so you will be able to ping to required host also you can run instead
exec("ping 1.2.3.4")
in your script