How to monitor wget command progress in ruby?

J

Joao Silva

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?
 
J

Joao Silva

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.
 
C

Choi, Junegunn

great....but i need explaination: what does "2>&1", "r"" mean? without

* '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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top