yegorpb
01-26-2007, 04:48 PM
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
$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?
Jeff - Exceed
01-26-2007, 05:21 PM
Try adding the verbose parameter, echo it out and then pipe it out to a text file.
yegorpb
01-26-2007, 06:27 PM
Hmm can you be more specific? Verbose parameter?
tiamak
01-26-2007, 10:07 PM
u can try something like this :D :
<?php
$a = popen('ping -c 20 www.google.com', 'r');
while($b = fgets($a, 2048)) {
echo $b."<br>\n";
ob_flush();flush();
}
pclose($a);
?>
Lightwave
01-27-2007, 02:14 AM
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.
yegorpb
01-27-2007, 02:32 AM
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?
Lightwave
01-27-2007, 03:04 AM
It could... or you could do it in php...
by lockfile i just mean a simple text file.
$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
#!/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
tiamak
01-27-2007, 10:10 AM
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 :D
yegorpb
01-28-2007, 02:07 AM
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?