没有 cron 的 PHP 调度程序
我想每 5 分钟运行一次 php 脚本。这是我的 PHP 代码。
function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
set_time_limit(0);
$root='http://mywebsiteurl'; //remote location of the invoking and the working script
$url=$root."invoker.php";
$workurl=$root."script.php";
call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
call_remote_file($url); //call again this script
我运行这段代码一次。即使我关闭整个浏览器窗口,它也能完美运行。
问题是如果我关闭系统的互联网连接,系统就会停止工作。
如何解决这个问题。请帮帮我。
I want to run my php script for every 5 minutes. Here is my PHP code.
function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
set_time_limit(0);
$root='http://mywebsiteurl'; //remote location of the invoking and the working script
$url=$root."invoker.php";
$workurl=$root."script.php";
call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
call_remote_file($url); //call again this script
I run this code once. It works perfectly, even after i close the entire browser window.
The problem is the stops working if i turn of my system's internet connect.
How to solve this problem. Please help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我真的不建议对一些关键的事情这样做(你会遇到稳定性问题),但这可能有效:
另一种方法是使用 exec() 从命令行调用它:
如果可能的话,你真的应该使用 cron 。
While I wouldn't really recommend doing this for something critical (you're going to have stability issues), this could work:
Another way would be to call it from the command line using
exec()
:You should really use cron though if at all possible.
上面的代码没问题,但是如果您想添加多个脚本以不同的时间间隔运行,那么编码就会变得更加复杂。
如果您尝试 phpjobscheduler (开源,可以免费使用),它提供了一个用于添加、修改和删除的界面要运行的脚本。
The above code is ok but if you want to add multiple scripts to run at different intervals then the coding becomes far more complicated.
If you try phpjobscheduler (open source so free to use) it provides an interface to add, modify and remove scripts to run.