没有 cron 的 PHP 调度程序

发布于 2025-01-04 18:00:08 字数 626 浏览 4 评论 0原文

我想每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱成瘾 2025-01-11 18:00:08

虽然我真的不建议对一些关键的事情这样做(你会遇到稳定性问题),但这可能有效:

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";

while(true)
{
    call_remote_file($workurl);//call working script
    sleep(60*5);// wait for 300 seconds.
}

另一种方法是使用 exec() 从命令行调用它:

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.
exec('php ' . $_SERVER['SCRIPT_FILENAME']);

如果可能的话,你真的应该使用 cron 。

While I wouldn't really recommend doing this for something critical (you're going to have stability issues), this could work:

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";

while(true)
{
    call_remote_file($workurl);//call working script
    sleep(60*5);// wait for 300 seconds.
}

Another way would be to call it from the command line using exec():

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.
exec('php ' . $_SERVER['SCRIPT_FILENAME']);

You should really use cron though if at all possible.

暗恋未遂 2025-01-11 18:00:08

上面的代码没问题,但是如果您想添加多个脚本以不同的时间间隔运行,那么编码就会变得更加复杂。

如果您尝试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文