开发者

Executing same php script consecutively

I'm trying to execute a script named csearch.php five times with the interval of 60 secs. I tried shell_exec and exec but it says, it can't fork, etc. I think the best way (since this is on a shared host) is by using include().

include('csearch.php');
sleep(60);
include('csearch.php');
sleep(60);
include('csearc开发者_StackOverflow中文版h.php');
sleep(60);
include('csearch.php');
sleep(60);
include('csearch.php');

csearch.php contains functions, etc. I'm wondering if this is possible (the above code). If not, what's the best way you can recommend? Thanks.

Edit, this is how I do it with exec and fails:

exec('/path/to/php /path/to/csearch.php');


Why would you want to do this? Sounds to me that you might have a need for a cronjob, or otherwise made a mistake in your script. Anyway, if the script is reachable from the "outside" (e.g. executable over HTTP), you might want to use cURL, as that can do the thing you request.

include('csearch.php');

$res = curl_init( 'http://site.com/csearch.php' );

while( $i < 5 ) {
   curl_exec( $res );
   sleep( 60 );
   $i ++;
}

But seriously, the chance that you actually need to do this is very slim.


I would take an approach similar to Berry's suggestion, but without the use of curl.

Wrap the code in csearch.php in a function (e.g. perform_action).

require_once('csearch.php');

for ($i = 0, $n = 5; $i < $n; $i++) {
    perform_action();

    if ($i < $n - 1) {
        // Only sleep if you need to
        sleep(60);
    }
}


You could use :

  • Zend Queue: http://www.zend.com/en/products/platform/product-comparison/job-queues
  • GearMan: http://www.php.net/manual/en/book.gearman.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜