有一个PHP函数,可以让你将脚本执行延迟到某个时间点.
所以我想说我有cron.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就是这个让你的梦想成真的框架. ).干杯.
编辑:如果您仍然需要有关计算正在运行的作业和/或删除(停止)它们的部分,我也可以帮助您解决它.只需回复我的帖子,我们就会跟进.