Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: output shell script output via php and web browser - HELP!

  1. #1
    Join Date
    Aug 2006

    output shell script output via php and web browser - HELP!

    Hello Gurus,

    How could I output the shell scripted output with PHP to a web browser?

    The script runs for 2 minutes and prints out several lines during the execution of the script on the command line.

    Need a way to run this with PHP and print the output of the shell script as the script executes instead of all at once when the script has completed execution.

  2. #2
    Join Date
    Mar 2007
    Las Vegas, NV
    I think there might be severeal ways to do this:


    Debian lenny

  3. #3
    Join Date
    Aug 2006
    Thanks, I have looked at all the docs for this type of Shell Command Execution. but they all behave the same. first they execute the program then display the output to the screen.

    I'm looking to display the output to the screen as the script executes.

    For example:

    I have a shell test script that I can run on the command line. first it will print the word "Hello", then wait 5 seconds and then print the word "Goodbye".

    // test.sh
    echo "hello";
    sleep 5;
    echo "goodbye";
    now using some of the PHP commands like exec() (and others) the script will execute and then display both hello and goodbye with no 5 second pause between the two

    PHP Code:
    $output exec('test.sh');
    is there a way to use PHP to display the output as the shell script is executing?

    so it should display "Hello" first then pause for five seconds and print "Goodbye", NOT execute for 5 seconds and display "Hello Goodbye".

    Is this example clear?

  4. #4
    Join Date
    Aug 2006
    Still looking for some help with this, anyone?

  5. #5
    Join Date
    Aug 2004
    You might be able to do it via AJAX.

    1. Main PHP script uses exec() or whatever to launch shell script as a background process with all output redirected to a file.

    2. JavaScript uses timer loop to call another PHP script that will read the file receiving the shell script's output, and returns it to the page where the JS can display it.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Join Date
    Mar 2009

    not working in browser

    I've tried the shell script code which mentioned in the above , I'm able to run the code in the command line . But in the browser its not displaying anything.Please tell me what may be the problem in executing the shell script via php in the browser.

    Thanks in advance

  7. #7
    Join Date
    Mar 2009

    not working in browser when try to open an application like firefox ,gvim etc.,

    In my previous comment i've mentioned that the shell script not executing in the browser via php. But when i'm trying to echo , its working . But when i try to open an application eg. firefox,gvim ,text editor , for some applications its returning the error . for some its displaying a white screen.

    if any one knows the answer, Please help me with some examples.

    Actually my requirement is to open a dreamweaver application thru php. My Server is running in gentoo linux and i've to open the dreamweaver in the windows machine which i'll access thru the rdesktop. If anyone knows the solution , please let me know.
    Last edited by ruby christina; 03-02-2009 at 08:47 AM.

  8. #8
    Join Date
    Aug 2006
    This could be a permissions issue with the launching of the programs. as the user that is running the script via web page is usually nobody or apache with limited permissions.

  9. #9
    Join Date
    Mar 2009

    getting error

    I've tried the above by changing the user and set full permissions to the user, still i'm getting a blank screen for opening the firefox and for opening gvim in linux the following error i'm getting

    Vim: Error reading input, exiting... Vim: Finished. [?25l[?12l[?25h

    this is my php code
    $output1 = shell_exec('chown ruby batcall.php startapp.sh');
    $output2 = shell_exec('chmod 755 batcall.php startapp.sh');
    //$output2 = shell_exec('ps aux | grep httpd');
    $output4 = shell_exec('sh startapp.sh');
    echo $output;


    Please tell me what is the solution for this

  10. #10
    Join Date
    Aug 2006
    How are you running the script?
    • Command line
    • Web page
    • Cron job

    If your running this on the command line than what user are you using? and does that user have the permission to change the permission on the files with chmod like you have in your script? User A can not chmod a file that User b owns.

    If you are executing this via a web page then most likely you're using the NOBODY or APACHE user which have very limited permissions to execute commands like the ones in your script. This is a security issue with the user(s). There is a way around this but I DO NOT recommend it. If you choose to do it anyway you set set the script you would like to execute permissions to 777 but this is VERY VERY BAD and I DO NOT suggest doing this.

    If your executing the script in cron you must check what user runs the cron job. If user A is trying to run a script that User B owns via cron it will fail. This again is a permission issue. Make sure your using the same user in cron as the owner of the script.

    Hope this helps, let me know any other errors you might be seeing so we could trouble shoot those as well.

  11. #11
    Join Date
    Aug 2006
    Also on another note you might want to try the full path to the file(s)

    PHP Code:
    // Yours
    $output1 shell_exec('chown ruby batcall.php startapp.sh');
    // My example
    $output1 shell_exec('chown ruby:ruby /path/to/batcall.php /path/to/startapp.sh');

    // Yours
    $output2 shell_exec('chmod 755 batcall.php startapp.sh');
    //My Example
    $output2 shell_exec('chmod 755 /path/to/batcall.php /path/to/startapp.sh');

    //$output2 = shell_exec('ps aux | grep httpd');

    // Yours
    $output4 shell_exec('sh startapp.sh');
    // My example
    $output4 shell_exec('sh /path/to/startapp.sh');

    // OR this example
    $output4 shell_exec('. /path/to/startapp.sh');


  12. #12
    Join Date
    Mar 2009

    not using cron jobs

    I've changed the user for batcall.php and startapp.sh files as root and permission is 777.

    my php code
    shell_exec('chown ruby:ruby /components/dream/batcall.php /components/dream/startapp.sh');
    shell_exec('chmod 777 /components/dream/batcall.php /components/dream/startapp.sh');

    $output = shell_exec('sh startapp.sh');
    //$output = shell_exec('ls -l');
    echo $output;

    but if i'm executing the code thru the web page , the user is still nobody and not executing . Tell me how to change the user while running the script via browser. I'm not using any cron jobs to run this. I'm using command line and webpage to run this.

    I'm trying these in gentoo linux .Actually my requirement is to open a dreamweaver application . My server is in gentoo linux. If i'm executing the same code in windows , the dreamweaver application has to open. I'm accessing the windows from linux thru rdesktop.

    Please provide me a solution for this.

  13. #13
    Join Date
    Aug 2006

    Well if you need the NOBODY user to execute the script you might try to chown the script to the nobody user on the server.

    chown nobody:nobody /path/to/script

    I still DO NOT suggest this as this will open up your system to anyone.

  14. #14
    Join Date
    Sep 2006
    Bucharest, RO
    ruby_christina: are you trying to open firefox on the server FROM the client? I think you are misunderstanding a few basic client-server issues. If you are simply running "firefox" using exec() or shell_exec() or any similar function in PHP and YOU are browsing from another PC, this will make the SERVER (not your desktop PC) try to open up firefox and, of course, you won't see anything on your PC (you can't run GNOME applications through HTTP :\). Also, the application won't even start work on your server because Apache is running in a different session (disregarding the user) which isn't connected to the XOrg server (the display server) so it can't run visual applications.

    On to Phill's problem. Well, it's a tricky one. I don't think any Ajax solution will work (there's still going to be a PHP script that will write to that file after 5 minutes). As I see it, you have two options:
    1) You create another user session and you get file pointers to its stdout, stdin and stderr and read / write from / to those in an unblocking way. But don't ask me how to do this
    2) You launch the command with an ampersand and you make it dump output to a file. Then you read the file. But, you're going to make a workaround in order to find out when the command has finished executing. For this, you can create a helper script that will output something when the big command finishes. Here's how I've done it:
    # helper.sh
    ping -c 4 google.com
    echo "FINISHED"
    PHP Code:
    // text/plain so we don't have any html weirdness + new lines are handled right
    header("Content-Type: text/plain");
    // make sure we output everything as soon as it comes in
    // run the command
    `./helper.sh > /var/www/work/test/tmp.txt &`;
    $fp fopen("tmp.txt"'r');
    if (!
    $fp) exit;
    // this is the string on which the command will be considered to have finished
    $magic_str "FINISHED";
    $full_str '';
    while ( 
    trim(substr($full_str, -strlen($magic_str)-1)) != $magic_str ) {
    $buf fread($fp1024);
    $full_str .= $buf;
    This works (tested), here are a few things to note:
    1) tmp.txt must exist and be readable/writable by Apache. It must exist because otherwise the file might not be yet created when PHP tries to fopen() it which will lead to an error.
    2) helper.sh must be executable (man that slowed me down a bit) by Apache.
    3) Lots of things can go wrong with this. Make sure you don't use it with just any command (you can create your own helper.sh through PHP then run it, if you want to run different commands using the same logic).
    4) Have fun
    Last edited by Znupi; 03-04-2009 at 11:00 AM.

  15. #15
    Join Date
    May 2009
    Hey guys, I've wanted to do this in the past but my will power gave out. But this time I was more persistent.

    I tried using the code Znupi posted but I couldn't get it to work.

    However, I went back to php.net and really read over the different manual pages on the system(), passthru(), exec(), shell_exec() functions and backticks operator.

    There is a BIG difference between using the system() or passthru() functions from using any of the others.

    This difference is how the output buffers of the command execution are handled.

    In system() and passthru(), PHP will attempt to flush the buffers after *every* line of output.

    This means that this code:

    PHP Code:
    system("echo \"Starting...\"
    sleep 2
    echo \"Done.\""
    will be treated just like this code written in only PHP:

    PHP Code:
    echo "Starting...";

    As long as you are not using output buffering on your page (ob_start()) then the effect will be exactly what you are looking for since the buffers will be flushed to the browser after each line of command output.

    But of course there is a caveat!

    It doesn't work with executing custom shell scripts! (well - not without a small workaround)

    The workaround is really simple though. Just use file_get_contents("myscript.sh") to turn the shell script contents into a command string. Insert that command string into your system() command and voila! The output buffers are flushed as expected.

    NOTE: One thing I did *not* test was the effect of this process on the working directory for the execution of the shell script commands. However, if your shell script relies on a particular working directory then make sure that the working directory is either set inside the shell script itself or that you do a "cd /custom/dir/here" (change directory) command just prior to where you insert the shell script command string.

    Example: (two files: test.php and test.sh)

    PHP Code:

    is_file($tmp_file)) {
    $fp fopen($tmp_file"w+");
    $fp) {
    $test_script file_get_contents("test.sh");
    system("echo \"Starting...\"
    sleep 2
    echo \"Doing more...\"
    echo \"Done.\""
    echo "Running test..."
    sleep 1
    echo "Almost done..."
    sleep 1
    echo "Test complete."

    And for those of you (like me) who are using output buffering on the entire page for whatever reason (templates, caching, error handling) - there is still a little more hacking to do. As long as your output is (X)HTML then the following solution will work for fully output buffered pages (ob_start()).

    Separate your system() and passthru() calls into a plain PHP file that just outputs plain text: (obviously make sure that you force some sort of user authentication prior to actually running the commands.php script!)

    PHP Code:
    ("Content-type: text/plain");

    $script file_get_contents("script.sh");
    then output HTML containing an iFrame:

    <iframe src="commands.php" scrolling="auto"></iframe>
    Good luck!

    Last edited by mongoose643; 05-07-2009 at 01:00 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13471 seconds
  • Memory Usage 3,061KB
  • Queries Executed 13 (?)
More Information
Template Usage (37):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (7)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates