Monday, 13 May 2013

Time out problem solved if using cron job


I was getting timeout while running cron job.  That problem is solved by following code. But problem is, it only works properly in cron job. If I open through browser, script will end abruptly. Anyway, we don't have time out problem using browser because we can use ajax.

$end_time = 600;//600 seconds is end time of script.
$pid = pcntl_fork();
if ($pid < 0)
exit;
else if ($pid) // parent
exit;
else {// child
$sid = posix_setsid();
if ($sid < 0){
exit;
}
for($i = 0; (time() - $end_time)<0; $i++) {
usleep($sleep_time);
                //do something, it will not time out.
}
}

For details, please visit,
http://php.net/manual/en/function.posix-setsid.php
More coming soon

No comments:

Post a Comment