Need command line to run a file 4 times

  • Thread starter jackster the jackle
  • Start date
J

jackster the jackle

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
 
X

Xavier Noria

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?

You mean something like this?

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

-- fxn
 
J

jackster the jackle

Xavier said:
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
 
X

Xavier Noria

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.

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
 
J

jackster the jackle

Xavier said:
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
 
S

Siep Korteling

=
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
 
X

Xavier Noria

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

Oh good.

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

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

-- fxn
 
J

jackster the jackle

Xavier said:
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
 
J

jackster the jackle

Siep said:
=

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
 
J

jackster the jackle

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
 
C

Carlos J. Hernandez

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
 
X

Xavier Noria

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.

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
 
J

jackster the jackle

Carlos said:
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
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
 

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

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top