Need command line to run a file 4 times

Hi Ruby Forum...

I have a script that I run every minute in cron to gather SNMP
statistics and put them in a DB.

Since CRON only goes down to one minute intervals and I'd rather not
change my code to loop so that I can poll every 15 seconds, is there a
way to run a ruby file from the command line and tell it to run the file
4 times?

something like this (which I know doesn't work)

do /usr/bin/SnmpPoll.rb * (4)

Thanks

jackster

http://jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.

You mean something like this?

    $ for x in 1 2 3 4; do echo $x; done
    1
    2
    3
    4

-- fxn

···

On Jan 11, 2008, at 2:47 PM, jackster the jackle wrote:

Since CRON only goes down to one minute intervals and I'd rather not
change my code to loop so that I can poll every 15 seconds, is there a
way to run a ruby file from the command line and tell it to run the file
4 times?

Xavier Noria wrote:

You mean something like this?

    $ for x in 1 2 3 4; do echo $x; done
    1
    2
    3
    4

-- fxn

Not exactly Xavier. I have a file (/usr/bin/snmp_poller.rb), when it
runs, it polls a network device and stores the SNMP data in the DB. I
currently have this running in CRON every minute. Without putting a loop
in my snmp_poller.rb code, I want to somehow tell CRON using a ruby
command line option, to run snmp_poller.rb 4 times.

I know there are a lot of command line, one liners to run ruby code and
I was hoping someone had one to do this.

thanks

jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.

Hmmm, but how is that different from chaging echo $x with your script? No trying to push that shell loop, just trying to understand the goal.

-- fxn

···

On Jan 11, 2008, at 5:00 PM, jackster the jackle wrote:

Xavier Noria wrote:

You mean something like this?

   $ for x in 1 2 3 4; do echo $x; done
   1
   2
   3
   4

-- fxn

Not exactly Xavier. I have a file (/usr/bin/snmp_poller.rb), when it
runs, it polls a network device and stores the SNMP data in the DB. I
currently have this running in CRON every minute. Without putting a loop
in my snmp_poller.rb code, I want to somehow tell CRON using a ruby
command line option, to run snmp_poller.rb 4 times.

I know there are a lot of command line, one liners to run ruby code and
I was hoping someone had one to do this.

Xavier Noria wrote:

Hmmm, but how is that different from chaging echo $x with your script?
No trying to push that shell loop, just trying to understand the goal.

-- fxn

It's actually not different, I was just looking for another way to do it
for control purposes.

jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.

Oh good.

Off the top of my head a possible approach would be:

    ruby -e '4.times { load "path/to/file" }'

-- fxn

···

On Jan 11, 2008, at 5:13 PM, jackster the jackle wrote:

Xavier Noria wrote:

Hmmm, but how is that different from chaging echo $x with your script?
No trying to push that shell loop, just trying to understand the goal.

-- fxn

It's actually not different, I was just looking for another way to do it
for control purposes.

Xavier Noria wrote:

Off the top of my head a possible approach would be:

    ruby -e '4.times { load "path/to/file" }'

-- fxn

That is exactly what I was looking for, Xavier! Thanks alot, that worked
perfectly.

jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.

Siep Korteling wrote:

=

It's actually not different, I was just looking for another way to do it
for control purposes.

jackster.mobi

from the commandline:

ruby -e '4.times{`/usr/bin/SnmpPoll.rb` ; sleep 15}'

regards,

Siep

Thanks for that added piece of information Siep, that might come in
handy for me.

jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.

My script is doing exactly what I want it to do now...but I see some
warnings in STDOUT:

warning: already initialized contstant <VARIABLE NAME HERE>

I think it's reporting this because ruby is looping through the file but
other than that, it's working fine.

Is there someway to fix this or at least prevent the warning from going
to the console?

thanks again

jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.

jacster:

You must be defining (assigning) the constant at one point in the
program flow, and
then defining again.
If the constant is in a loop, take it out.
I typically define all my constants within the first few lines at the
top of the file right after the require statements.

Ruby only warns that you're using a constant as a variable.
If you mean to use it as a variable, don't use capital letters.

-Carlos

···

On Sat, 12 Jan 2008 03:03:50 +0900, "jackster the jackle" <contact@thirdorder.net> said:

My script is doing exactly what I want it to do now...but I see some
warnings in STDOUT:

warning: already initialized contstant <VARIABLE NAME HERE>

I think it's reporting this because ruby is looping through the file but
other than that, it's working fine.

Is there someway to fix this or at least prevent the warning from going
to the console?

thanks again

jackster.mobi
--
Posted via http://www.ruby-forum.com/.

Indeed, that is the case with load. In the command line you can silence warnings with -W0, like this:

     fxn@feynman:~$ ruby -e 'C = C = 1'
     -e:1: warning: already initialized constant C
     fxn@feynman:~$ ruby -W0 -e 'C = C = 1'
     fxn@feynman:~$

-- fxn

···

On Jan 11, 2008, at 7:03 PM, jackster the jackle wrote:

My script is doing exactly what I want it to do now...but I see some
warnings in STDOUT:

warning: already initialized contstant <VARIABLE NAME HERE>

I think it's reporting this because ruby is looping through the file but
other than that, it's working fine.

Carlos Hernandez wrote:

jacster:

You must be defining (assigning) the constant at one point in the
program flow, and
then defining again.
If the constant is in a loop, take it out.
I typically define all my constants within the first few lines at the
top of the file right after the require statements.

Ruby only warns that you're using a constant as a variable.
If you mean to use it as a variable, don't use capital letters.

-Carlos

On Sat, 12 Jan 2008 03:03:50 +0900, "jackster the jackle"

Good call Carlos... my problem was that I was defining my variables
using some capital letters and didn't realize that was set aside for
constants.
I really appreciate the help!

jackster.mobi

···

--
Posted via http://www.ruby-forum.com/.