沃梦达 / 编程问答 / php问题 / 正文

调度php脚本

Scheduling php scripts(调度php脚本)

本文介绍了调度php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create some function to schedule php scripts,for example if i want tu run page.php at 12/12/2012 12:12 i can call

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

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

or for example call one script every minute

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

我可能会添加一些其他功能来查看调度了哪些脚本或删除其中一个.

i'll may add some other function to see what script are scheduled or delete one of them.

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

i want this functions to work on both UNIX and WINDOWS platforms,i DO NOT want ugly solutions like executing a script on every page of the site(i want to schedule this commands when nobody is on the site) or using "buisy wait" implementations ( using sleep() on a script that checks if there are any scheduled jobs) or something that require user intervention(like write something in console or open a panel).

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

I found the "AT" command on MSDOS(works well on all windows)but it's very basic because it accept only time and not dates,there's a more powerful version on UNIX but i don't know how to use it(and i want a solution for both platforms).

推荐答案

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

There's a PHP function which lets you delay script execution till a point in time.

假设我有 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]);
       }

?>

还有你的 classes/functions 文件:

// 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 是这个框架,可以让你的梦想成真......更快.;)干杯.

It ought to work. By the way, K2F is this framework that makes your dreams come true..faster. ;). Cheers.

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

If you still want the parts about counting running jobs and/or deleting(stopping) them, I can help you out with it as well. Just reply to my post and we'll follow up.

这篇关于调度php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:调度php脚本

基础教程推荐