gpt4 book ai didi

php - 调度php脚本

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:30 24 4
gpt4 key购买 nike

我想创建一些函数来安排 php 脚本,例如,如果我想在 12/12/2012 12:12 运行 page.php,我可以调用

schedule_script('12/12/2012 12:12','page.php');//or by passing a time/datetime object

或者例如每分钟调用一个脚本

schedule_interval(60,'page.php');//every 60s=1minute

我可能会添加一些其他功能来查看安排了哪些脚本或删除其中一个。

我希望这个功能在 UNIX 和 WINDOWS 平台上都能工作,我不想要丑陋的解决方案,比如在网站的每个页面上执行脚本(我想在没有人在网站上时安排这个命令)或使用“buisy等待”实现(在脚本上使用 sleep() 来检查是否有任何计划的作业)或需要用户干预的东西(比如在控制台中写东西或打开面板)。

我在 MSDOS 上找到了“AT”命令(在所有 Windows 上都运行良好)但它非常基础,因为它只接受时间而不接受日期,在 UNIX 上有一个更强大的版本,但我不知道如何使用它(我想要一个适用于两个平台的解决方案)。

最佳答案

有一个 PHP 函数可以让您将脚本执行延迟到某个时间点。

假设我有 cron.php:

<?php

// Usage:
// cron.php [interval|schedule] [script] [interval|stamp]
if(!isset($argc) || count($argc)!=2)die; // security precaution

$time=(int)$argv[3]; // just in case :)

if($argv[1]=='schedule'){
time_sleep_until((int)$_GET['until']);
include_once($time);
}elseif($argv[1]=='interval')
while(true){ // this is actually an infinite loop (you didn't ask for an "until" date? can be arranged tho)
usleep($time*1000); // earlier I said milliseconds: 1000msec is 1s, but this func is for microseconds: 1s = 1000000us
include_once($argv[2]);
}

?>

还有你的/函数文件:

// Const form K2F - Are we on windows?
define('ISWIN', strpos(strtolower(php_uname()),'win')!==false &&
strpos(strtolower(php_uname()),'darwin')===false );

// Function from K2F - runs a shell command without waiting (works on all OSes)
function run($cmd){
ISWIN ? pclose(popen('start /B '.$cmd,'r')) : exec($cmd.' > /dev/null &');
}

script_schedule($script,$time){
if(is_string($time))$time=strtotime($time);
run('php -f -- schedule '.escapeshellarg($script).' '.$time);
}

script_interval($script,$mseconds){
run('php -f -- interval '.escapeshellarg($script).' '.$mseconds);
}

它应该有效。顺便说一下,K2F 是一个让你梦想成真的框架......更快。 ;).干杯。

编辑:如果您仍然想要有关计算正在运行的作业和/或删除(停止)它们的部分,我也可以帮助您。只需回复我的帖子,我们就会跟进。

关于php - 调度php脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com