Ruel
Ruel

Reputation: 15780

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('csearch.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');

Upvotes: 0

Views: 148

Answers (3)

Nick Clark
Nick Clark

Reputation: 4467

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

Upvotes: 0

Berry Langerak
Berry Langerak

Reputation: 18859

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.

Upvotes: 1

Related Questions