Page 1 of 1

Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 2:03 pm
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 :)

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 2:12 pm
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

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 2:14 pm
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');

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 2:41 pm
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.

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 2:52 pm
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...

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 3:02 pm
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?

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 3:04 pm
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.

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 3:09 pm
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).

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 3:29 pm
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 &');

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 3:46 pm
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.

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 3:49 pm
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? :(

Re: Browser waiting for CLI script to finish

Posted: Sat Jan 26, 2008 4:06 pm
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)"