shell_exec() and friends terminate PHP script (or time out?)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.