LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell_exec() and friends terminate PHP script (or time out?) (https://www.linuxquestions.org/questions/programming-9/shell_exec-and-friends-terminate-php-script-or-time-out-480096/)

MicahCarrick 09-03-2006 04:13 PM

shell_exec() and friends terminate PHP script (or time out?)
 
I do love this forum...

Ihave a PHP script which needs to execute a system command which runs for about 7 minutes. I need the PHP script to wait until that command completes and then continue running. However, after the command completes, the script terminates.

I have a call to set_time_limit(0); and safemode is Off. When I run a command that executes quickly, the script works fine. When I have a loop that runs some code (such as link validators) where the code may be executing in a loop for as long as 10 minutes it works fine. It seems to be specific to PHP waiting idly for the system call to return. I've tried exec, shell_exec, backtick operator-- they all do the same thing.

Suggestions?

Guttorm 09-04-2006 06:02 AM

Hi

If the PHP script is running on a web-server, it will most likely timeout when the browser disconnects, it will do that if it gets no response from the script in a certain time.

If you call ignore_user_abort() the script will continue, even if you cannot make any output. For more info:
http://www.php.net/manual/en/feature...n-handling.php

Another solution is to have the script make some output every once in a while.

Hope this helps
Guttorm

MicahCarrick 09-04-2006 10:56 PM

So here's the code.
Code:

  set_time_limit(0);

  // ...

  $temp = `cp -r quick $filename`;
  if (empty($temp)) echo "OK<br />";
  else echo "ls: $temp<br />";

The "if (empty($temp)) echo "OK<br />" line is never executed. The backtick operator for the copy command takes 7 minutes. So I can't output to browser periodically (as I'm waiting for the command to complete). ignore_user_abort() doesn't really apply as I'm leaving the browser open and running not hitting the stop button.

Perhaps I'm going to have to somehow pipe the command and poll it's process ID waiting for the command to complete whilst still running PHP in a loop so it doesn't time out?

Guttorm 09-05-2006 04:37 AM

Hi again

I think, if all you need is to have the script finish i.e. you dont have to give any response to the browser when it's done, you can just use ignore_user_abort() - if you read the manual carefully, you'll see that the script stops executing when it times out, even when you don't hit the stop button. It will timeout when the broser has not recieved any output in a certain time. (I think it's a browser setting.)

Another way of doing it:
exec('echo "cp somebigfile somewhereelse" |at now');
This can't time out, and will continue, even if the webserver is stopped. You might have to add the apache user (www-data?) to /etc/at.allow for it to work.

And then, just output to the browser that it has started. If you click refresh, you can make a test in the script somewhere above, comparing the file sizes, and if different, say that work is in progress, and give the % of done.


All times are GMT -5. The time now is 02:44 PM.