Unable to Timeout Exec

This script sometimes hangs like 20mins until server PHP will show timeout error, and I don’t know how to set the time limit to kill it. set_time_limit doesn’t do anything. I have also read that exec is missing direct timeout command.

$argv="http://google.com";  ///some URL'S stay hanging
 

$command = "wkhtmltoimage $argv test.jpg";
///set_time_limit doesn't work
set_time_limit(5);
exec($command, $output, $ret);
if ($ret) {
    echo "error fetching screen dump\n";
    die;
}

Please help, Thank you

The problem is exec is a blocking command (it pauses execution until it finishes). You need to run the command slightly differently. On Linux, suffixing the command with & will actually put the output as the command after &. You will also need to redirect both stdout and stderr to /dev/null/. On windows you can use the start command:

Linux:

$command = "wkhtmltoimage $argv test.jpg > /dev/null 2>/dev/null &";

Windows:

$command = "start wkhtmltoimage $argv test.jpg";
1 Like

Thank you, I like the Linux version, this works perfectly and really “much faster” than before(too fast maybe)… but I ran into other problems with this, because this script is part of a bigger script and it’s too fast. So sleep(5); after it will fix everything for now.

Thank you again!

I have noticed that if using > /dev/null 2>/dev/null & on some bigger and slower sites the command fails, lack of time to load the site or something. Without the command > /dev/null 2>/dev/null & works the slower sites too, but then some sites may hang… I haven’t found a 100% working solution… are there any simple suggestions to make this simple script work?

Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.