I\'m looking for a way to run a PHP process with a timeout. Currently I\'m simply using exec(), but it does not provide a timeout option.
What I also tried is openin
Improving on other solutions I came up with this:
function exec_timeout($cmd,$timeout=60){
$start=time();
$outfile=uniqid('/tmp/out',1);
$pid=trim(shell_exec("$cmd >$outfile 2>&1 & echo $!"));
if(empty($pid)) return false;
while(1){
if(time()-$start>$timeout){
exec("kill -9 $pid",$null);
break;
}
$exists=trim(shell_exec("ps -p $pid -o pid="));
if(empty($exists)) break;
sleep(1);
}
$output=file_get_contents($outfile);
unlink($outfile);
return $output;
}