473,473 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

running an exec in the background, let page go on.

I've a PHP script that does some stuff, zips up some files, and starts
an FTP process.

I need it to do the Zip and FTP in the background and let the page
finish, otherwise the user could have that page sitting there loading
for 20 minutes while the FTP completes.

Here's what I have:

$zipfile = $order_po.".zip";
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status);
echo "<div align='center' class='style1_hd'>Your auto-created ZIP file
is currently being sent through FTP!</div></body></html>";
exit();
?>

(The ftp.sh is the FTP logon and transfer script.)
As you can see I have the & to make it a background process, and
sending the stdoutput elsewhere.

Any ideas?

Thanks!
Liam

Apr 27 '06 #1
11 3096
Ksu
run it in new window

Apr 27 '06 #2
Ksu wrote:
run it in new window

I'm sorry? Run it in a new window?
As in a new browser window?
That doesn't really solve the problem, just moves the problem to
another browser window which must likewise remain open for 10 to 40
minutes while the FTP completes.

Maybe there is no answer. =/
-Liam

Apr 27 '06 #3
ne**@celticbear.com wrote:
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status); Why doesn't this work?
As you can see I have the & to make it a background process, and
sending the stdoutput elsewhere.

Try pcntl_fork().

Apr 27 '06 #4

Sjoerd wrote:
ne**@celticbear.com wrote:
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status);

Why doesn't this work?


I have no idea, which is why I'm asking here. =)
As you can see I have the & to make it a background process, and
sending the stdoutput elsewhere.

Try pcntl_fork().


Evidently that doesn't work when PHP is an Apache module, which is the
case for me. =/

Thanks for the reply! =)
-Liam

Apr 27 '06 #5

ne**@celticbear.com wrote:
Maybe there is no answer. =/


Maybe you can try something that isn't pure PHP. Have you considered
AJAX? When the page loads, you can send out an asynchronous call to
the server which would have your process in a seperate php file. That
php file could post back progress to the original page at intervals so
that you could display progress to the user without blocking the data
transfer.

Joseph

Apr 27 '06 #6
ne**@celticbear.com wrote:
I've a PHP script that does some stuff, zips up some files, and starts
an FTP process.

I need it to do the Zip and FTP in the background and let the page
finish, otherwise the user could have that page sitting there loading
for 20 minutes while the FTP completes.

Here's what I have:

$zipfile = $order_po.".zip";
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status);
echo "<div align='center' class='style1_hd'>Your auto-created ZIP file
is currently being sent through FTP!</div></body></html>";
exit();
?>

(The ftp.sh is the FTP logon and transfer script.)
As you can see I have the & to make it a background process, and
sending the stdoutput elsewhere.

Any ideas?

Thanks!
Liam


Im not quite sure if it is the neatest solution but for these kind of
operations I create a todo file/table/etc and have crontab check the
file every x mins and then execute the a script based on the parameters
given in the todo file. You can catch the output and store it wherever u
like.

Arjen

Apr 28 '06 #7
On Thu, 27 Apr 2006 09:33:08 -0700, Sjoerd wrote:
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status);

Why doesn't this work?


Because the stdout handle will still point to the handle the Apache/PHP
module is listening/blocking for input on.

I posted more about this in my recent thread:

Executing PHP Tasks While Letting A User Continue To Browse?
http://nextgen.url123.com/backgroundphp
(Google groups link for those of you that can't find it in your newsreader)

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Apr 28 '06 #8
On Fri, 28 Apr 2006 08:07:13 +0000, Andy Jeffries wrote:
On Thu, 27 Apr 2006 09:33:08 -0700, Sjoerd wrote:
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status);

Why doesn't this work?


Because the stdout handle will still point to the handle the Apache/PHP
module is listening/blocking for input on.

I posted more about this in my recent thread:


Sorry, wrong thread in the earlier message, I meant this one:

Daemonising to continue asynchronously
http://nextgen.url123.com/asyncphp
(Google groups, blah, blah).

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Apr 28 '06 #9
On Thu, 27 Apr 2006 09:23:35 -0700, ne**@celticbear.com wrote:
run it in new window

I'm sorry? Run it in a new window?
As in a new browser window?
That doesn't really solve the problem, just moves the problem to another
browser window which must likewise remain open for 10 to 40 minutes
while the FTP completes.

Maybe there is no answer. =/


Don't lose hope, you definitely can do it. As per my earlier pointer, you
could do it by execing another PHP script that detaches from the parent
process and closes stdout (I posted code in that thread to do it) then
take your long action from there.

Another advantage is you don't need to worry about trying to use & to make
your .sh exec go in the background from that PHP script, so you could
print errors from the .sh, capture them in the PHP script and mail them to
you if you like or log them.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Apr 28 '06 #10
have a wrapper script which you call via your php script..

in there have something like this:

***
nohup var/www/html/services/ftp.sh $zipfile &
***
This way the process will detatch itself from the script, let the
script complete and all id dandy..

Apr 28 '06 #11
In <11**********************@g10g2000cwb.googlegroups .com>,
"ne**@celticbear.com" <ne**@celticbear.com> mentions:
I need it to do the Zip and FTP in the background and let the page
finish, otherwise the user could have that page sitting there loading
for 20 minutes while the FTP completes.

Here's what I have:

$zipfile = $order_po.".zip";
exec("/var/www/html/services/ftp.sh $zipfile 2>&1 &", $output2,
$status);
echo "<div align='center' class='style1_hd'>Your auto-created ZIP file
is currently being sent through FTP!</div></body></html>";
exit();
?>


If it's the kind of thing that DOESN'T need to be distributed and
it it's a UNIX based host..

I would take a page from the cron suggestion, but, attempt to run it
through the 'batch' command. (On some hosts, batch is run from cron, the
two are kind of related)

Batch will handle all the nitty gritty about emailing you when it's done, etc..
Plus, it's nice to the system, waiting for a time when the machine isn't doing
a whole lot to run the commands. I use batch a LOT for this kind of thing.
(forking processes that need to "go away" when done) Easy on the system, no
worries about zombie processes or signal handlers, etc...

Problem: Batch might not like to be run as user 'nobody' so you
may have to pull some tricks to get around that. This is doable
with set-ID, but, it's less then ideal.

Unfortunately, batch one of those commands that is not well utilized.

If you can get it to work, batch is by far the best, since it won't be
connected to the web server in any way.

Lets hear it for batch! :-)

If it does need to be distributed or batch won't work, you can do
it in perl, and call on the fork() method (after closing stdio)

Or, just redirect stdio to and from /dev/null Forking in apache
is tricky business, especially when mysql handles are open and
such.

Jamie
--
http://www.geniegate.com Custom web programming
gu******@lnubb.pbz (rot13) User Management Solutions
May 1 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
8
by: Sticks | last post by:
ok... im not quite sure how to describe my problem. i have a php script that runs through my entire php site and writes the resulting output to html files. this is necessary as the nature of the...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
1
by: Anonieko | last post by:
Query: How to display progress bar for long running page Answer: Yet another solution. REFERENCE: http://www.eggheadcafe.com/articles/20050108.asp My only regret is that when click the...
4
by: Shailesh Humbad | last post by:
I was trying to exec a background process on XP using PHP CLI, but could not get it to work. Suppose the command I want to spawn off is "cmd". On *nix, it is as easy as putting ampersand "&" at...
5
by: awalter1 | last post by:
Hi, I develop a graphical user interface (with pyGTK) where a click on a button shall launch a program P in background. I want to get the end of this program P but I don't want that my HMI be...
4
by: benwylie | last post by:
I am running IIS 6.0 on Windows 2003. I would like to be able to run a perl script from a web page and include the output. I have tried doing it with an ssi: <form action='docsearch.shtml'...
8
by: Ravi | last post by:
Hi to all, There is a start button in my page. if user clicks on that then a php program should start and should listen on a particular port. and also user should able to do other tasks on...
5
by: This | last post by:
I have a pretty basic emailing script that sends a relatively small number (150) of html emails. The emails are compiled, personalised from a mysql db subscribers list, and sent using mail() -...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.