# Getting Ruby to output a windows command to text (log) file

**URL:** <https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865>\
**Category:** Ruby\
**Created:** [September 17, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865 "2008-09-17T20:54:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 17, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/1 "2008-09-17T20:54:09Z")

</div>

Hey folks, I’m trying to create a program to ping one of my company’s  
computers every minute to measure latency, and output the results to a  
text file. I’ve been able to figure out / cobble together most of what  
I need (see attached ping.rb file) but the problem i’m having is I  
cannot figure out how to get Ruby to copy the results from the ping to  
the text file that is created.

What I get currently when I run the program (from the log file):

* * *

Wed Sep 17 13:52:45 -0500 2008  
System Ping Monitor:

Wed Sep 17 13:52:45 -0500 2008

* * *

The “ping” command doesn’t output the way I need it to, HALP!!

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 17, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/2 "2008-09-17T21:59:24Z")

</div>

If you need to also get at stderr … take a look at popen3

ilan

Tony Mcneil wrote:

> * * *
> 
> Wed Sep 17 13:52:45 -0500 2008  
> System Ping Monitor:
> 
> Wed Sep 17 13:52:45 -0500 2008
> 
> * * *
> 
> The “ping” command doesn’t output the way I need it to, HALP!!

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 17, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/3 "2008-09-17T21:40:38Z")

</div>

See [http://www.ruby-doc.org/core/classes/Kernel.html#M006001](http://www.ruby-doc.org/core/classes/Kernel.html#M006001) :

`cmd` =\> string

Returns the standard output of running cmd in a subshell. The built-in  
syntax %x{…} uses this method. Sets $? to the process status.

`date` #=\> “Wed Apr 9 08:56:30 CDT 2003\n”  
`ls testdir`.split[1] #=\> “main.rb”  
`echo oops && exit 99` #=\> “oops\n”  
$?.exitstatus #=\> 99

gegroet,  
Erik V.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 17, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/4 "2008-09-17T22:08:42Z")

</div>

Erik V. wrote:

> See [http://www.ruby-doc.org/core/classes/Kernel.html#M006001](http://www.ruby-doc.org/core/classes/Kernel.html#M006001) :
> 
> `cmd` =\> string
> 
> Returns the standard output of running cmd in a subshell. The built-in  
> syntax %x{ï¿½} uses this method. Sets $? to the process status.
> 
> `date` #=\> “Wed Apr 9 08:56:30 CDT 2003\n”  
> `ls testdir`.split[1] #=\> “main.rb”  
> `echo oops && exit 99` #=\> “oops\n”  
> $?.exitstatus #=\> 99
> 
> gegroet,  
> Erik V.

hrm, I’ve tried looking over the documentation in the link you  
provided, but I’m having difficulty understanding it. Could you provide  
an example of the code?

thanks again,  
~Tony

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 17, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/5 "2008-09-17T23:38:07Z")

</div>

Tony Mcneil wrote:

> Erik V. wrote:
> 
> > See [http://www.ruby-doc.org/core/classes/Kernel.html#M006001](http://www.ruby-doc.org/core/classes/Kernel.html#M006001) :
> > 
> > `cmd` =\> string
> 
> hrm, I’ve tried looking over the documentation in the link you  
> provided, but I’m having difficulty understanding it. Could you provide  
> an example of the code?
> 
> thanks again,  
> ~Tony

This won’t work (even when you remove the typo .to\_S):  
f.puts system(‘ping 192.168.1.74’).to\_S

The ping will be executed, but you are logging if it succeeded, not the  
output.  
Erik is pointing to a working solution:

f.puts `ping 192.168.1.74`

Note these `` are backticks, not single quotes.

hth,

Siep

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 17, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/6 "2008-09-17T22:02:14Z")

</div>

Thanks much Erik! I still need to study that link / see if I can figure  
out exactly what that means, but that should work perfectly, thanks  
again!

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [September 18, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/getting-ruby-to-output-a-windows-command-to-text-log-file/148865/7 "2008-09-18T15:46:01Z")

</div>

> This won’t work (even when you remove the typo .to\_S):  
> f.puts system(‘ping 192.168.1.74’).to\_S
> 
> The ping will be executed, but you are logging if it succeeded, not the  
> output.  
> Erik is pointing to a working solution:
> 
> f.puts `ping 192.168.1.74`
> 
> Note these `` are backticks, not single quotes.
> 
> hth,
> 
> Siep

Thank you! that was the bit that made the difference (backtick instead  
of quote) The .to\_S was more of an experiment that I forgot to pull  
immediately 🙂

thanks for all your help everyone!
