Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239

    exec() function display in real time?

    Im trying to build in some things I normally do via SSH into the CP of my site. I have a bash script, that takes the content of 1 folder, and mirrors it to 6 different servers. This operation takes a few minutes, and in SSH it shows the transfer rates, file sizes, and completion percentages for each file, thats being uploaded to each server. So after it uploads 1 file, it starts a new line with the next file. When the files are all mirrored to 1 server, it does the same process for server 2, 3 4....

    How would I output this in real time, onto a web page?

    this code only displays a "snapshot" of the executed code, but I wanna see the progress as it happens.

    PHP Code:
    <?php
    $data 
    = array();                // define array

    exec('ls -l'$data$ret);     // execute command, output is array

    echo "<pre>";
    if (
    $ret == 0) {                // check status code. if successful
        
    foreach ($data as $line) {  // process array line by line
            
    echo "$line \n";
        }
    } else {
        echo 
    "Error in command";    // if unsuccessful display error
    }
    echo 
    "</pre>";
    ?>
    is there any way to do it?

  2. #2
    Try adding the verbose parameter, echo it out and then pipe it out to a text file.
    BeeServe
    * Rock solid shared & reseller UK webhosting. No downtime™ *
    Now offering fully managed VPS servers

  3. #3
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239
    Hmm can you be more specific? Verbose parameter?

  4. #4
    Join Date
    Oct 2004
    Location
    Złocieniec, Poland
    Posts
    190
    u can try something like this :
    PHP Code:
    <?php
    $a 
    popen('ping -c 20 www.google.com''r');

    while(
    $b fgets($a2048)) {
    echo 
    $b."<br>\n";
    ob_flush();flush();
    }
    pclose($a);
    ?>
    www.goscinnawies.pl - family business, small travel agency in Poland

  5. #5
    Join Date
    Apr 2003
    Location
    San Jose, CA.
    Posts
    1,622
    You'll probably want to do something like creating a lock file in the script.

    Check for the lock file.
    If the lock file exists then a previous instance might still be running. Abort.
    If no lock file is found... do stuff.
    Remove lock file.

    Otherwise, with code like the above... your hitting refresh or reload... would end up with multiple similar processes running simultaneously. For a simple command like ls or ping -c 20 it wouldnt matter too much... but with something designed to copy files to multiple servers... you probably wouldn't ever want more then one copy running simultaneously.

  6. #6
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239
    You are absolutely right. Once the script started executing, I dont want it to stop if I refresh, or run another copy. I want it to display the output of the running script, until it finishes executing.

    Im not sure what you mena bylock file? Im not too fluent in bash scripting, it was created by my server admin. Would this have to be added to the bash script?

  7. #7
    Join Date
    Apr 2003
    Location
    San Jose, CA.
    Posts
    1,622
    It could... or you could do it in php...

    by lockfile i just mean a simple text file.

    PHP Code:
    $filename '/path/to/foo.txt';

    if (
    file_exists($filename)) {
        print 
    "The file $filename exists, script already running???";
        exit(); 
    /* or return error to calling program */
    } else {
        print 
    "The file $filename does not exist, safe to run...";


    or

    PHP Code:
    #!/bin/bash
    if [ -f mylockfilefile ]
    then
      
    echo mylockfile exists!
      exit 
    1;
    fi

    #must not have existed... safe to create it.. and continue...

    touch  ./mylockfile
    #do stuff here
    rm ./mylockfile 

  8. #8
    Join Date
    Oct 2004
    Location
    Złocieniec, Poland
    Posts
    190
    hmm i dont like lockfiles

    anyway
    u can use proc_open (instead of popen)
    you can set it to output all info to fifo file

    and then proc_get_status to get pid
    then save pid in some session variable

    then before proc_open add code (ps aux | grep ourpidhere | grep -v grep) to check for pid if we have any
    if process is working switch to code that only read from fifo
    else run proc_open

    i dont have time for writing code but it could look like this:
    if(!checkpid) {
    make fifo
    proc_open...
    proc_get_status...
    save pid
    read from fifo
    }
    else {
    read from fifo
    }

    anyway everything is clearly explained in proc_open, popen (that fifo part), proc_get_status php manual
    www.goscinnawies.pl - family business, small travel agency in Poland

  9. #9
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239
    Interesting, logging to a txt file and then displaying the contents of that file as the script executes seems the best option for me, but Im still rather sketchy on the execution issue. After I execute the command, the browser just hangs there until the command is executed in bash (which takes a few minutes). Is there a way to keep it running in the background, and simply forward me to a page that will have the txt file echoed (which I can pull up with ajax every second for live feedback), before it completes the execution of the command?

Posting Permissions

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