Browser waiting for CLI script to finish

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Browser waiting for CLI script to finish

Post by alex.barylski »

I am invoking a script from CLI using exec* family of functions and everything is working great - except that the CLI script doesn't return squat to screen and it's infinite so it never ends like. :P

The problem this causes, is that the browser hangs forever, waiting for the CLI script to hault execution and return something...

Is there anyway I can get around this caveat - without resorting to process control extensions???

p.s-I have already tried using a trailing '&' to force the process into the background. This doesn't do the trick!!! :(

p.s.s-I should note that I don't want to use process control extensions because they are not available on Windows and likely never will be. So no command line hacks in forcing the script into daemon mode or anything.

p.s.s.s-I am already considering using Javascript to get around this little annoyance. :P

Cheers :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Browser waiting for CLI script to finish

Post by VladSun »

Hockey wrote:p.s-I have already tried using a trailing '&' to force the process into the background. This doesn't do the trick!!! :(
I.e.?
Hockey wrote:p.s.s-I should note that I don't want to use process control extensions because they are not available on Windows and likely never will be. So no command line hacks in forcing the script into daemon mode or anything.
viewtopic.php?f=1&t=72617&p=411214
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Browser waiting for CLI script to finish

Post by Christopher »

Have you thought of somehow adding a polling mode to your script so PHP can run it?

Code: Select all

$result = exec('myscript --poll');
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Browser waiting for CLI script to finish

Post by alex.barylski »

I.e.?
Not sure I follow you VladSun? :?

When I add the trailing '&' the browser still hangs waiting for the result of the script, if thats what you mean?
Have you thought of somehow adding a polling mode to your script so PHP can run it?
I'm not sure I follow you either. :P

When I call exec() family of functions from a normal PHP script (ie: Apache module) the script executes as expected, but the browser hangs because it's waiting for the invoked script to finish executing - I think.

So I'm not sure how implementing polling would do the trick?

Edit: VladSun I would rather not have a conditional check for Windows or Linux as it's more work and it's dependent on extensions being installed. Right now it's not an issue as I have direct control over the server but in the future that may not be the case.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Browser waiting for CLI script to finish

Post by VladSun »

Hockey wrote:
I.e.?
Not sure I follow you VladSun? :?

When I add the trailing '&' the browser still hangs waiting for the result of the script, if thats what you mean?
I meant - "what happens" instead of "This doesn't do the trick!!!"

I've tried this:

Code: Select all

<?php shell_exec('ping yahoo.com > /dev/null &'); ?>
and it works...
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Browser waiting for CLI script to finish

Post by alex.barylski »

Code: Select all

<?php shell_exec('ping yahoo.com > /dev/null &'); ?>
By works you mean what exactly? When I invoke the script like so via the browser and my web site index.php:

Code: Select all

echo 'Output: '.shell_exec('php scripts/cleanup.php &');
The browser hangs...waiting for the results I assume. Not sure what I'm missing in my command?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Browser waiting for CLI script to finish

Post by alex.barylski »

Here is my cleanup.php script

Code: Select all

    while(1){
 
        sleep(ELAPSED_TIME);
    }
If I remove the infinite loop...obviously the script finishes quickly and the browser doesn't hang...but the hope is that I can invoke the script like shown above and have it run endlessly until shutdown via kill or other.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Browser waiting for CLI script to finish

Post by VladSun »

Hockey wrote:

Code: Select all

echo 'Output: '.shell_exec('php scripts/cleanup.php &');
The browser hangs...waiting for the results I assume. Not sure what I'm missing in my command?
Did you notice the '> /dev/null' - this means that *any* output to STDOUT will be ignored.
You can't expect that browser will not hang while you are *waiting* for output from a process (it doesn't matter whether it's in background or in foreground).
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Browser waiting for CLI script to finish

Post by alex.barylski »

So:

Code: Select all

echo 'Output: '.shell_exec('php > scripts/cleanup.php &');
Should do the trick?

Edit: I tried this and although the browser doesn't hang...the simple loop doesn't seem to want to keep the process alive. When is ps -aux I don't see the script process???

Code: Select all

 
#!/usr/bin/php
<?php
 
    ignore_user_abort(true);
 
    while(1){
 
        sleep(5);
    }
 
 
This is currently all that my cleanup script does...am I missing anything?

Invoked like:

Code: Select all

echo shell_exec('php > scripts/queue.php &');
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Browser waiting for CLI script to finish

Post by VladSun »

Code: Select all

shell_exec('php -q scripts/queue.php 2>&1 > /dev/null &');
I did stress on that you could't get the ouput. So, echoing it doesn't make any sense.
Last edited by VladSun on Sat Jan 26, 2008 4:03 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Browser waiting for CLI script to finish

Post by alex.barylski »

VladSun wrote:

Code: Select all

echo shell_exec('php -q scripts/queue.php 2>&1 > /dev/null &');
I did stress on that you could't get the ouput. So, echoing it doesn't make any sense.
Fare enough, but is there any reason my script is not staying an executing process even with an infinite loop? When I ps without the '>' the script process is clearly visible. When I kill it and then re-start it using '>' the script doesn't send output and the browser doesn't hang, but the process seems to execute and stop. When I ps in the latter case - no more process? :(
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Browser waiting for CLI script to finish

Post by VladSun »

Code: Select all

command > name
means "send output of command to file name"

Code: Select all

command 2> &1 > name
means "send output of command and "output for errors" to file name"

I used

Code: Select all

command 2> &1 > /dev/null
and it means "send output of command and "output for errors" to "blackhole" (i.e. ignore it)"
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply