Forum: Ruby How to monitor wget command progress in ruby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2009-02-09 01:03
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?
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-02-09 01:10
(Received via mailing list)
exec("command")
system("command")
result = `command`
pipe = IO.popen("command", "r")

See:

http://whynotwiki.com/Ruby_/_Process_management
8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2009-02-09 01:21
Dan Diebolt 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?
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-02-09 01:33
(Received via mailing list)
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
8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2009-02-09 01:49
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.
69f776698d1a63c70af26bfaea085b17?d=identicon&s=25 Choi, Junegunn (Guest)
on 2009-02-09 04:09
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.