计划php脚本 [英] Scheduling php scripts

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

问题描述

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

  schedule_script('12 / 12/2012 12:12','page.php'); //或通过传递时间/日期时间对象



或例如每分钟呼叫一个脚本

  schedule_interval(60,'page.php'); //每60s = 1分钟

可能会添加一些其他函数来查看调度的脚本或删除其中一个脚本。



我希望此功能在UNIX和WINDOWS平台上工作,我不想丑陋解决方案,例如在网站的每个页面上执行脚本(我想在没有人在网站上时调度此命令)或使用buisy等待实现(在检查是否有任何计划作业的脚本上使用sleep());



我在MSDOS上找到了AT命令(在所有窗口上都可以正常工作),但它是一个需要用户干预(比如在控制台或打开一个面板)非常基本,因为它只接受时间而不是日期,在UNIX上有一个更强大的版本,但我不知道如何使用它(和我想要两个平台的解决方案)。

解决方案

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



cron.php

 <?php 

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

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

if($ argv [1] =='schedule'){
time_sleep_until((int)$ _ GET ['until']);
include_once($ time);
} elseif($ argv [1] =='interval')
while(true){//这实际上是一个无限循环(你没有要求一个until布置tho)
usleep($ time * 1000); //以前我说的毫秒:1000msec是1s,但这个func是微秒:1s = 1000000us
include_once($ argv [2]);
}

?>

您的 / functions file:

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

//来自K2F的函数 - 运行一个shell命令而不等待(适用于所有操作系统)
function run($ cmd){
ISWIN? pclose(popen('start / B'。$ cmd,'r')):exec($ cmd。'> / dev / null&');
}

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

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

它应该工作。顺便说一下,K2F是这个框架,使你的梦想成真。 ;)。 Cheers。



编辑:如果您仍然希望零件有关于计算正在运行的作业和/或删除(停止)与它一起。只要回覆我的讯息,我们就会跟进。


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.

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).

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).

解决方案

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

So let's say I have 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]);
       }

?>

And your classes/functions file:

// 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);
}

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

Edit: 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脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆