LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-03-2006, 04:13 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
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?
 
Old 09-04-2006, 06:02 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
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
 
Old 09-04-2006, 10:56 PM   #3
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
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?
 
Old 09-05-2006, 04:37 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run PHP shell_exec as root maneeshsethi Linux - General 5 12-30-2008 07:25 AM
How to get the program to terminate after sertain length of time? hubabuba Programming 6 04-23-2006 11:06 PM
PHP exec() and shell_exec() and SELinux Cottsay Programming 1 06-25-2005 09:47 PM
PHP: what's wrong with this line (shell_exec, find, xargs, grep)? J_Szucs Programming 3 11-19-2003 07:44 PM
PHP last time modified script time way off GabeF Programming 2 11-05-2002 08:05 PM


All times are GMT -5. The time now is 09:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration