我有一个包含主机和端口的数据库,我想知道在严格的 PHP 中是否有一种方法可以一次 ping 所有这些并每 30 分钟记录一次。
编辑:忘了添加..我是初学者
最佳答案
你可以只使用像 cron 这样的任务调度程序,但如果你真的想在脚本本身内以指定的时间间隔重复执行任务,你可以将循环与 sleep()
组合在一起。或 usleep()
:
while (true) {
/*
Perform pings here and write output to some file
If the functions write directly to stdout, you can use
ob_start() and ob_get_clean() to catch the output
and write it to a log file instead.
*/
sleep(1800); // 30 minutes in seconds
}
使用此方法,您应该在 php.ini 中或使用 set_time_limit(0)
禁用 PHP 的时间限制设置。
关于PHP 一次和定时间隔 Ping 多个 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556195/