Cronjob to emulate asynchronous calls

Discussion in 'PHP' started by grutland, Jun 24, 2011.

  1. #1
    Hi,

    I'm a developer at a website monitoring system and currently have a lot of cronjobs checking a number of different sites for a number of different things.

    Each check seems to be taking an average 5 seconds (just as an example) and the obvious eventual problem is that there will be too many sites and not enough time.
    Does any one know a way of running multiple cronjobs at the same time with out clogging up the Crontab file?

    An idea I've had is to use PHP's exec() function, but not sure how viable that actually is as I haven't had a chance to test it yet.
    As a very quick example though, this is a rough example of what I would like to achieve:

    foreach($sites as $site){
    exec('php /path/to/script.php '. $site);
    }
    PHP:
    The way I passed in the parameter in there may be wrong, but it's just an example.

    Shameless plug
    The system in question is actually currently in BETA and in desperate need of testers, if any one would be interested please take a look at http://www.mysitevoice.com/
     
    grutland, Jun 24, 2011 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    I think exec will also wait for the response before continuing. You could try adding multiple entries into the crontab, not the most scalable but might work as a plan b.

    Another option is using popen since that doesn't wait for the response. I've used it for Windows scheduled tasks before but I don't think there's anything Windows specific in it...

    foreach($sites as $site){
        $pptr = popen("start /b php /path/to/script.php ".$site.", "r"); 
        pclose($pptr);
    }
    PHP:
     
    mfscripts, Jun 24, 2011 IP