How to monitor wget command progress in ruby?


#1

Hello! I currently developing rails application but it’s more
ruby-specyfic question. I have background process launched in starling.
All aplication work is a downloading large files in background. I use
wget to do this. At current i launching simple

system(‘wget file etc’)

But i need to monitor this progress. Wget produces output like this
durning downloading:

100%[======================================>] 30 241 120K/s in
0,2s

How i can read this?


#2

exec(“command”)
system(“command”)
result = command
pipe = IO.popen(“command”, “r”)

See:

http://whynotwiki.com/Ruby_/_Process_management


#3

Dan D. wrote:

exec(“command”)
system(“command”)
result = command
pipe = IO.popen(“command”, “r”)

See:

http://whynotwiki.com/Ruby_/_Process_management

Thanks, but i still don’t know how i can read from wget console?


#4

Look at the chart on that page. If you want to read character by
character wget’s stdout

pipe = IO.popen(“command”, “r”)

Sorry I don’t have time to write an example but this should point your
in the right direction


#5

Great! I was write code:

f = IO.popen(“wget http://www.hostgator.com/testfile.zip 2>&1”, “r”) {
|pipe|
pipe.each do |line|
sleep 1.5
puts line
end
}

and it’s almost done - it’s outputs something like this:

8350K … … … … … 41% 230K
54s
8400K … … … … … 41% 226K
54s
8450K … … … … … 41% 229K
53s
8500K … … … … … 41% 221K
53s
8550K … … … … … 41% 234K
53s
8600K … … … … … 42% 226K
53s
8650K … … … … … 42% 233K
53s
8700K … … … … … 42% 222K
52s
8750K … … … … … 42% 233K
52s
8800K … … … … … 43% 224K
52s
8850K … … … … … 43% 222K
52s
8900K … … … … … 43% 237K
51s
8950K … … … … … 43% 229K
51s
9000K … … … … … 44% 230K
51s
9050K … … … … … 44% 229K
51s
9100K … … … … … 44% 222K
51s

great…but i need explaination: what does “2>&1”, “r”" mean? without
it code:

f = IO.popen(“wget http://www.hostgator.com/testfile.zip”, “r”) { |pipe|
pipe.each do |line|
sleep 1.5
puts line
end
}

Didn’t work. I would like avoid copypaste coding, i found 2>&1 in some
tutorial.


#6

great…but i need explaination: what does “2>&1”, “r”" mean? without
it code:

  • ‘2>&1’ says that standard error stream (whose file desecriptor is 2)
    will be redirected
    to the same place where the standard output stream is sent. Since wget
    sends its output
    to standard error, you need this redirection to capture the output using
    pipe.

Take the following commands with pipes as an example.

ls / | wc
30 30 181
ls no_such_path | wc
ls: cannot access no_such_path: No such file or directory
0 0 0
ls no_such_path 2>&1 | wc
1 9 64

Incidentally, ‘2>&1’ is often used to suppress error messages and
standard output.

some_command > /dev/null 2>&1

  • ‘r’ means you are opening the pipe in read-only mode.