Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts

    PHP to Wait on Shell Script Requiring User Input

    I have a shell script that requires user input. This script executes xwininfo, which is a utility for the X server that waits for the user to click on a window so that it can return info about it.

    When I use PHP's exec, shell_exec, or system functions, they all execute the script as if xwininfo was never called. My guess is that when PHP executes a shell script, it can't transfer the focus to the script, and thus it never waits for user input and goes on like it doesn't exist.

    My question is whether I can execute a shell script from a web page that gains the focus, receives user input, and outputs its info, while PHP waits for the script to finish before serving the page back to the browser? This just doesn't seem possible from my testing so far.

    Any guidance would be great. Thanks.

    -Shane

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts

  • #3
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    I haven't tried it yet, but the only difference between the passthru function and the exec function is just output. You get raw binary data with the passthru. I'm not looking for that. I'm looking for the PHP server to wait on serving the page while the shell script takes focus and does what it needs to do first.

    -Shane

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by TheShaner View Post
    I haven't tried it yet, but the only difference between the passthru function and the exec function is just output. You get raw binary data with the passthru. I'm not looking for that. I'm looking for the PHP server to wait on serving the page while the shell script takes focus and does what it needs to do first.

    -Shane
    This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser
    that's all what you get, this is not Star Wars, and php is not the Force,

    when you run xwininfo all mouse events will be directed to the x server untill somebody will click somewhere on that server( the one who run x not the one with the browser if there are different) and then, probably, the output of xwininfo will apear in browser.
    I didn't try and I'm not sure about this scenario but this is how I think will work,

    best regards

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by TheShaner View Post
    I haven't tried it yet, but the only difference between the passthru function and the exec function is just output. You get raw binary data with the passthru. I'm not looking for that. I'm looking for the PHP server to wait on serving the page while the shell script takes focus and does what it needs to do first.

    -Shane
    back after a short test. I was wrong about how it work. It don't do nothing.
    script:
    PHP Code:
    <?php
    passthru
    ('/usr/bin/xwininfo',$result);
    echo 
    $result;
    ?>
    will output 1 and in xwininfo man page I didn't find when xwininfo return a status like that.
    sorry for negative answer but look to the bright side of that story, you don't waste time with this and you can focus to search another way to achive what you want,

    best regards

  • #6
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by oesxyl View Post
    that's all what you get, this is not Star Wars, and php is not the Force,
    Unnecessary comment. I'm well aware of most of the capabilities and limitations of PHP and other server-side languages. I'm not looking for PHP to do any magic; just execute a shell script that requires a mouse event in order to finish processing.

    Quote Originally Posted by oesxyl View Post
    when you run xwininfo all mouse events will be directed to the x server untill somebody will click somewhere on that server( the one who run x not the one with the browser if there are different) and then, probably, the output of xwininfo will apear in browser.
    I didn't try and I'm not sure about this scenario but this is how I think will work,
    You're not understanding my dilemma. The problem is not receiving output from the script. I already have web pages that run shell scripts and output back to the browser. My problem is that when PHP executes xwininfo, it does not wait for a mouse event and/or cannot detect the mouse event. My belief is that there is an issue with a web page calling a shell script that requires user input or interactions, i.e. mouse events.

    In my experience, when a shell script is executed from within PHP, the server will wait for the script to finish before the page is served up again. There are ways around having your server wait for a script to finish before serving it up (which I've also done before), but in this case, I would actually like that to happen. I want the server to wait for the script to finish executing. The script should not finish until you 'left click' a window.

    In my test cases, however, when PHP executes the script, there is no opportunity to 'left click'. It's as if xwininfo never executes inside the script. I can't figure out whether this is because PHP has problems calling scripts that require user input/events, or if it has something to do with a user/permission problem of the apache user possibly. The apache server and PHP module all run on the same box that I'm attempting to use this script on. And yes, the script works when it's called from the command line.

    So what I'm looking for is anyone with experience with having PHP call a shell script that requires user input and/or mouse events. It may or may not be possible, and so I'm hoping there is someone out there that has tried something similar before.

    Thanks,

    Shane

  • #7
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by oesxyl View Post
    PHP Code:
    <?php
    passthru
    ('/usr/bin/xwininfo',$result);
    echo 
    $result;
    ?>
    When you ran that test, were you given the typical + icon and a chance to select a window before it echoed back your result? Did the server wait for you to click? Theoretically, I would think that your browser would show that it's loading as your server waits for you to click a window so that it can finish executing the xwininfo command and serve up your page.

    My guess is it didn't do that for you and that is exactly the dilemma I'm having.

    -Shane

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by TheShaner View Post
    When you ran that test, were you given the typical + icon and a chance to select a window before it echoed back your result? Did the server wait for you to click? Theoretically, I would think that your browser would show that it's loading as your server waits for you to click a window so that it can finish executing the xwininfo command and serve up your page.

    My guess is it didn't do that for you and that is exactly the dilemma I'm having.

    -Shane
    it's no dilemma here, when you run a script in browser it run in a different envoirment.
    try this:
    PHP Code:
    <?php
    exec
    ('/usr/bin/xwininfo',$result,$output,$result);
    print_r($output);
    echo 
    $result;
    ?>
    run in the browser, result will be:
    Code:
    Array ( ) 1
    run same script from comand line with php -f script-name and will wait until you click on something and output the result. with a 0 status, that mean ok.
    the problem is that runing in the browser it can't find x server.
    Anyway have no sense what you want because x server run on one server, let's say A, and the browser can run on another server, let's say B separated by net.
    how do you expect to click with your mouse on B and process the event on A?

    I don't know if is what you want but for remote x apps there is alread a mechanism. Let's me know if this is what you intend to do.

    best regards

  • #9
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by oesxyl View Post
    it's no dilemma here, when you run a script in browser it run in a different envoirment.
    try this:
    PHP Code:
    <?php
    exec
    ('/usr/bin/xwininfo',$result,$output,$result);
    print_r($output);
    echo 
    $result;
    ?>
    run in the browser, result will be:
    Code:
    Array ( ) 1
    Exactly. That is the problem for me in which I'm trying to find a solution to. The xwininfo command failed and gave you that output. I can't have that.

    Quote Originally Posted by oesxyl View Post
    run same script from comand line with php -f script-name and will wait until you click on something and output the result. with a 0 status, that mean ok.
    I figured it probably would, but wasn't sure since I haven't tested that yet.

    Quote Originally Posted by oesxyl View Post
    the problem is that runing in the browser it can't find x server.
    Now this may be the case. I need to do some more testing. I need to find a way for a web browser to execute xwininfo.

    Quote Originally Posted by oesxyl View Post
    Anyway have no sense what you want because x server run on one server, let's say A, and the browser can run on another server, let's say B separated by net.
    how do you expect to click with your mouse on B and process the event on A?

    I don't know if is what you want but for remote x apps there is alread a mechanism. Let's me know if this is what you intend to do.
    This project is an internal project. It will not be on the internet. The web page will always be displayed on the same box as the apache server and X server. There will not be any remoting when utilizing this page and script.

    -Shane

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by TheShaner View Post
    I need to find a way for a web browser to execute xwininfo.

    This project is an internal project. It will not be on the internet. The web page will always be displayed on the same box as the apache server and X server. There will not be any remoting when utilizing this page and script.
    look over expect, maybe could help you:

    http://www.php.net/manual/en/book.expect.php

    unfortunately I don't have installed this extension and I can't do tests.

    best regards

  • #11
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by oesxyl View Post
    look over expect, maybe could help you:

    http://www.php.net/manual/en/book.expect.php

    unfortunately I don't have installed this extension and I can't do tests.
    I'll have to see if I can install that extension for testing.

    Here is my test case called test.php:
    PHP Code:
    <?php
        ini_set
    ('display_errors'1);
        
    ini_set('error_reporting'E_ALL);

        
    $cmd 'xwininfo | grep xwininfo | grep "Window id:" | sed s/[^\"]*// | sed s/\"//g';
        
    $output shell_exec($cmd);
        echo 
    'Window Title: ' trim($output) . "\n";
    ?>
    If you run it command line with php -f test.php, it works perfectly.

    However, if you bring it up in a web browser on the same machine, you never get a chance to click a window and of course get an empty window title.

    -Shane


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •