2

I have a command that when run direct on the command line works as expected. It runs for over 30 seconds and does not throw any errors. When the same command is called through a PHP script through the php function exec() (which is contained in a script called by a cron) it throws the following error:

Maximum execution time of 30 seconds exceeded

We have a number of servers and i have run this command on a very similar server with the exact same dataset without any issues so i'm happy there is no script-level issue. I'm becoming more inclined to think this is related to something at the server level - either in the PHP setup or the server setup in some way but really not sure where to look. For those that are interested both servers have a max execution time of 30 seconds.

the command itself is called like this -

from command line as:

root@server>php -q /path/to/file.php

this works...

and via cron within a PHP file as:

exec("php -q /path/to/file.php");

this throws the max execution time error. it was always my understanding that there was no execution time limit when PHP is run from the command line.

I should point out that the script that is called, calls a number of other scripts and it is one of these scripts that is erroring. Looking at my logs, the max execution time error actually occurs before 30 seconds has even elapsed too! So, less than 30 seconds after being called, a script, called by a cron script that appears to be running as CLI is throwing a max execution error.

To check that the script is running as i expected (as CLI with no max execution time) i performed the following check:

A PHP script containing this code:

// test.php
echo exec("php test2.php");

where test2.php contains:

echo ini_get('max_execution_time');

and this script is run like this:

root@server> php test.php
// returns 0

This proves a script called in this way is running under CLI with a max execution time of 0 which just proves my thoughts, i really cannot see why this script is failing on max execution time!

robjmills
  • 18,438
  • 15
  • 77
  • 121
  • What happens if you run the PHP script from the commandline, not from cron? – Vinko Vrsalovic Aug 11 '10 at 16:36
  • You say "when run direct on the command line works fine" but that is not enough information: despite working fine from the shell prompt, does it take anywhere near the 30 seconds that your PHP's max/exec/time setting (forget the exact name) is apparently set to? – Dexygen Aug 11 '10 at 16:37
  • @George - i had assumed that running the script through exec() was the equivalent to running it under CLI - is that not the case? – robjmills Aug 11 '10 at 17:16
  • I down-modded this question because despite being asked numerous times how long the script takes to execute from the command line, for some reason he refuses to answer. – Dexygen Aug 11 '10 at 20:10
  • @George - bit harsh since i've been afk, i'm not refusing to answer anything!. running the script from the command line takes over 30 seconds but its not as simple as that given the script that is called calls a script that in turn calls a number of other scripts and its a called script that throws the error – robjmills Aug 11 '10 at 20:22
  • well you answered me once in the meantime and I had asked this and you hadn't answered. if you had provided this in the original question, instead of "works fine" from the command line, then when any number of us thought to suggest increasing the max_execution_time parameter, we could have done so with reasonable confidence it was definitive. If on the other hand "works fine" meant it ran in just a few seconds, not 30+, then it would have probably required a different solution altogether. – Dexygen Aug 11 '10 at 23:29
  • @George - i have added as much detail as i can. feel free to ask any more questions you have – robjmills Aug 12 '10 at 08:22

7 Answers7

1

it seems that your script takes too much time to execute, try to

set time limit, http://php.net/manual/en/function.set-time-limit.php

or check this post: Asynchronous shell exec in PHP

Community
  • 1
  • 1
Andy Lin
  • 545
  • 2
  • 4
1

Does the command take over 30 seconds on the command line? Have you tried increased the execution timeout in the php.ini?

Joshua D. Drake
  • 1,026
  • 5
  • 6
1

You can temporarily set the timeout by including this at the top of the script. This will not work when running in safe mode as is specified in the documents for setting max_execution_time with ini_set().

<?php
    ini_set('max_execution_time', 60);  // Set to be longer than 
                                        // 60 seconds if needed

    // Rest of script...
?>

One thing of note in the docs is this:

When running PHP from the command line the default setting is 0.

Buggabill
  • 13,726
  • 4
  • 42
  • 47
1

What does php -v | grep cli, run from both the shell and in the exec command from the cron-loaded php file show?

Does explictly typing /usr/bin/php (modify as appropriate) make any difference?

Jhong
  • 2,714
  • 22
  • 19
  • adding /usr/bin/php gives the same result – robjmills Aug 12 '10 at 08:46
  • That's odd -- you tried `exec('php -v | grep cli');` from within the PHP file run by cron? I agree it shouldn't be timing out. Does this server have e.g. the Suhosin patch applied? Do you have separate php.inis for php and php-cli? – Jhong Aug 12 '10 at 08:46
  • according to out admin we have one main php.ini and dont have the suhosin patch – robjmills Aug 12 '10 at 09:10
  • Stumped then: sorry :-). I'd be inclined to suspect a bug in your version of php-cli. Does `php -d max_execution_time=0 -q /path/to/file.php` work? – Jhong Aug 12 '10 at 10:03
  • ok, tried that - now getting "Maximum execution time of 0 seconds exceeded" ! – robjmills Aug 12 '10 at 11:07
  • sorry, maybe `php -d max_execution_time= -q /path/to/file.php` ? – Jhong Aug 12 '10 at 12:39
  • please see my answer - the issue is something to do with `max_input_time` – robjmills Aug 12 '10 at 13:04
1

I've actually found what the issue is (kinda). It seems that its maybe a bug with PHP reporting max_execution_time to be exceeded when the error is actually with max_input_time as described here

I tried changing the exec call to php -d max_execution_time=0 -q /path/to/file.php and i got the error "Maximum execution time of 0 seconds exceeded" which makes no sense, i changed the code to be php -d max_input_time=0 -q /path/to/file.php and the code ran without erroring. Unfortunately, its still running 10 minutes later. At least this proves that the issue is with max_input_time though

robjmills
  • 18,438
  • 15
  • 77
  • 121
0

I'm surprised that no one above has actually timed the completed exec call. The problem is that exec(x) is taking a much longer time than command line x. I have a very complex perl script (with 8 levels of internal recursion) that takes about 40 sec to execute from the command line. Using exec inside a php script to call the same perl program takes about 300 sec to execute, i.e., a factor of about 7X longer time. This is such an unexpected effect that people aren't increasing their max execution time sufficiently to see their programs complete. As a result, they are mystified by the timeout. (BTW, I am running on WAMP in a fast machine with nominally 8 cpus, and the rest of my php program is essentially trivial, so the time difference must be completely in the exec.)

user2684428
  • 165
  • 1
  • 6
0

create wrapper.sh file as below

export DISPLAY=:0<br>
xhost + 2>>/var/www/err.log<br>
/usr/bin/php "/var/www/read_sms1.php" 2>>/var/www/err.log<br>

and put it in cron as below

 bash  /var/www/wrapper.sh<br>
y read_sms1.php  contain<br>
$ping_ex = exec("/usr/local/bin/gnokii --getsms SM 1 end  ", $exec_result, $pr);  

and above solution workedfine for me in ubuntu 12.04

Community
  • 1
  • 1
BIJU KV
  • 89
  • 1
  • 2