0

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');
5
  • How are you doing your exec()? This is how you should do it and it should work. Plus, you can't include() the same script... Commented Feb 28, 2011 at 13:46
  • 1
    what about exec('/usr/bin/php /path/to/php /path/to/csearch.php'); ? Commented Feb 28, 2011 at 13:48
  • @Shikiryu - you can include the same script. That's why PHP also provides include_once() for cases where you don't want to. Commented Feb 28, 2011 at 13:50
  • @Shikiryu: you can include a file as many times as you want, but if it's got function defnitions in there, you'll get a fatal error from trying to redeclare the same function. Commented Feb 28, 2011 at 14:38
  • @Marc B : that was my point. The OP clearly said csearch.php contains functions, etc.. What I meant with can't include the same script... Sorry for the misunderstanding. Commented Feb 28, 2011 at 14:40

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

You could use :

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.