I have a cron job which runs every minute and executes internal cron logic for a multi tenanted application. The PHP script gets each client, their domains and then forks itself and runs under each client "scope" (internal name). See below:
<?php
$i = 0;
foreach($clients as $client) {
$client->get_domains();
foreach($client->domains as $domain) {
$threads[$i]['client'] = $client->id;
$threads[$i]['domain'] = $domain->id;
$i++;
}
}
$pids = array();
foreach($threads as $key => $thread) {
$pids[$key] = pcntl_fork();
if(!$pids[$key]) {
$start = microtime(TRUE);
$handle = popen($args[0] . ' --client ' . $thread['client'] . ' --domain ' . $thread['domain'] . ' 2>&1', 'r');
// $end1 = ~0.001 sec execution time
pclose($handle);
// $end2 = ~60 sec execution time
exit();
}
}
I'm currently migrating load to another server, so have a fresh Ubuntu 11.04 install and set up like the existing machine. There are around 40 domains running on the app, so 40 forks are created within a second.
My issue is that event with each of the forked processes simply returning TRUE
the processes are taking around 60 seconds.
When dumping $end1
time, the popen() is taking around 0.001 seconds, as to be expected. However, when monitoring $end2
the pclose() is taking ~60 seconds.
Watching htop, the processes are visible, but seem to be paused. There are 4 CPU's and 16GB RAM in the VM and load on the server is under 0.1 while running. Running PHP 5.3.6 on Ubuntu 11.10.
I have a feeling that it is something on the OS / PHP config but ulimit is normal and PHP memory limit and max execution time are unlimited. Is there anything else I should be checking?