Can I keep re-running the same script? how?

I want to run a particular script every 3 minutes. I would do this
using crontab in Linux, but I am having trouble getting that to work.
The script works fine from the Windows or Linux command line, however.

I thought it might be easier to add some code to the end of the script
that tells it to sleep for three mins and then start at the beginning.

Any thoughts? As always, I appreciate your help.

-Hunter

···

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

sleep 180
exec ["ruby" + ARGV].join(' ')

-a

···

On Wed, 7 Jun 2006, Hunter Walker wrote:

I want to run a particular script every 3 minutes. I would do this
using crontab in Linux, but I am having trouble getting that to work.
The script works fine from the Windows or Linux command line, however.

I thought it might be easier to add some code to the end of the script
that tells it to sleep for three mins and then start at the beginning.

Any thoughts? As always, I appreciate your help.

-Hunter

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

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

In practice I would say this is a bad idea. What if you start this on a server and forget about it? Then, some day down the road, the process starts failing for whatever reason, and some poor admin is left wondering whether or not it's safe to kill the process. Call me paranoid.

Better to figure out why it's failing under cron. The most likely causes, in my experience, tend to be environment variable issues and bad assumptions about what Dir.pwd is.

Regards,

Dan

···

ara.t.howard@noaa.gov wrote:

On Wed, 7 Jun 2006, Hunter Walker wrote:

I want to run a particular script every 3 minutes. I would do this
using crontab in Linux, but I am having trouble getting that to work.
The script works fine from the Windows or Linux command line, however.

I thought it might be easier to add some code to the end of the script
that tells it to sleep for three mins and then start at the beginning.

Any thoughts? As always, I appreciate your help.

-Hunter

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

sleep 180
exec ["ruby" + ARGV].join(' ')

-a

Not to be contrarian, but pickaxe page 180 says that ARGV[0] is not the
name of the program (unlike in C). There's other stuff I don't get
about your snippet as well.
Why ["ruby" + ARGV]??? Seems like you're using + to add an array to a
string? Can you explain it for us newbies?

Working from Pickaxe page 180 I would have come up with something like:
sleep 180 # obvious
pieces = [ruby, $0].concat(ARGV) # $0 is program name
exec pieces.join(' ') # to string and execute it

BTW, many rubyist's would, I think, stick the .join(' ') on the end of
the array composition, like this:
exec [ruby, $0].concat(ARGV).join(' ')

Is there a move away from such Perlish pursuits, or are you still
considered a "Sheila" if you split things like this up into more easily
understandable pieces?

thanks,
jp

unknown wrote:

···

On Wed, 7 Jun 2006, Hunter Walker wrote:

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

sleep 180
exec ["ruby" + ARGV].join(' ')

-a

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

dan is right of course. i didn't say it was a good idea - but you can do it
:wink:

-a

···

On Wed, 7 Jun 2006, Daniel Berger wrote:

sleep 180
exec ["ruby" + ARGV].join(' ')

-a

In practice I would say this is a bad idea. What if you start this on a server and forget about it? Then, some day down the road, the process starts failing for whatever reason, and some poor admin is left wondering whether or not it's safe to kill the process. Call me paranoid.

Better to figure out why it's failing under cron. The most likely causes, in my experience, tend to be environment variable issues and bad assumptions about what Dir.pwd is.

Regards,

Dan

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

Jeff Pritchard wrote:

Not to be contrarian, but pickaxe page 180 says that ARGV[0] is not the
name of the program (unlike in C). There's other stuff I don't get
about your snippet as well.
Why ["ruby" + ARGV]??? Seems like you're using + to add an array to a
string? Can you explain it for us newbies?

Working from Pickaxe page 180 I would have come up with something like:
sleep 180 # obvious
pieces = [ruby, $0].concat(ARGV) # $0 is program name
exec pieces.join(' ') # to string and execute it

BTW, many rubyist's would, I think, stick the .join(' ') on the end of
the array composition, like this:
exec [ruby, $0].concat(ARGV).join(' ')

Is there a move away from such Perlish pursuits, or are you still
considered a "Sheila" if you split things like this up into more easily
understandable pieces?

thanks,
jp

unknown wrote:

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

sleep 180
exec ["ruby" + ARGV].join(' ')

-a

I just got an error when I tried the code below:

exec ["ruby" + ARGV].join(' ')

I'll try the code you suggested, JP. Thanks!

···

On Wed, 7 Jun 2006, Hunter Walker wrote:

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

Not to be contrarian, but pickaxe page 180 says that ARGV[0] is not the
name of the program (unlike in C). There's other stuff I don't get
about your snippet as well.
Why ["ruby" + ARGV]??? Seems like you're using + to add an array to a
string? Can you explain it for us newbies?

sure. a mistake :wink:

should have been

   sleep 180
   exec ['ruby', __FILE__, ARGV].flatten.join(' ')

Working from Pickaxe page 180 I would have come up with something like:
sleep 180 # obvious
pieces = [ruby, $0].concat(ARGV) # $0 is program name

sometimes:

   harp:~ > ruby -e' $0 = "foobar"; p $$; system "ps -elf|grep foobar|grep -v grep" '
   12128
   0 S ahoward 12128 4377 0 75 0 - 707 wait4 21:45 pts/2 00:00:00 foobar

regards.

-a

···

On Wed, 7 Jun 2006, Jeff Pritchard wrote:
--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

Hi,

At Wed, 7 Jun 2006 11:22:00 +0900,
Jeff Pritchard wrote in [ruby-talk:196251]:

pieces = [ruby, $0].concat(ARGV) # $0 is program name
exec pieces.join(' ') # to string and execute it

You don't need join here.

  exec *pieces

is safe even if ARGV contains spaces.

···

--
Nobu Nakada

I agree 100%. I just want to get something up and running while I
figure out the cron issue.

Below is my thread on the subject in the Ubuntu forums if you have any
ideas:

http://ubuntuforums.org/showthread.php?p=1104031#post1104031

Thanks again for your respones!

···

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

I would either fix cron or use
at(http://en.wikipedia.org/wiki/At_(Unix_command)\)

While all these options work, in a production environment I wouldnt
feel right with a scheduling system of some sort. autosys/cron/at etc.

···

On 6/6/06, Hunter <walkerhunter@gmail.com> wrote:

Jeff Pritchard wrote:
> Not to be contrarian, but pickaxe page 180 says that ARGV[0] is not the
> name of the program (unlike in C). There's other stuff I don't get
> about your snippet as well.
> Why ["ruby" + ARGV]??? Seems like you're using + to add an array to a
> string? Can you explain it for us newbies?
>
> Working from Pickaxe page 180 I would have come up with something like:
> sleep 180 # obvious
> pieces = [ruby, $0].concat(ARGV) # $0 is program name
> exec pieces.join(' ') # to string and execute it
>
> BTW, many rubyist's would, I think, stick the .join(' ') on the end of
> the array composition, like this:
> exec [ruby, $0].concat(ARGV).join(' ')
>
> Is there a move away from such Perlish pursuits, or are you still
> considered a "Sheila" if you split things like this up into more easily
> understandable pieces?
>
> thanks,
> jp
>
> unknown wrote:
>> On Wed, 7 Jun 2006, Hunter Walker wrote:
>>
>>>
>>> --
>>> Posted via http://www.ruby-forum.com/\.
>>
>> sleep 180
>> exec ["ruby" + ARGV].join(' ')
>>
>> -a

I just got an error when I tried the code below:

exec ["ruby" + ARGV].join(' ')

I'll try the code you suggested, JP. Thanks!

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

unknown wrote:

Hi,

At Wed, 7 Jun 2006 11:22:00 +0900,
Jeff Pritchard wrote in [ruby-talk:196251]:

pieces = [ruby, $0].concat(ARGV) # $0 is program name
exec pieces.join(' ') # to string and execute it

You don't need join here.

  exec *pieces

is safe even if ARGV contains spaces.

I got the cron working by doing two things. Using Tar2RubyScript and
then the crontab syntax below:

*/5 * * * * ruby /var/www/quickbase/sae_sync/auto_sync.rb

Before I had:

*/5 * * * * /var/www/quickbase/sae_sync/ruby auto_sync.rb

That being said, I am going to learn how to create some .sh scripts and
I think that will be useful. Thank you for all the help.

···

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

What I usually do in cases like this is something like:

  * * * * * /path/to/bash script-to-setup-environment-and-run-my-program.sh

It allows me significantly more control.

-austin

···

On 6/6/06, Hunter Walker <walkerhunter@gmail.com> wrote:

I agree 100%. I just want to get something up and running while I
figure out the cron issue.

Below is my thread on the subject in the Ubuntu forums if you have any
ideas:

http://ubuntuforums.org/showthread.php?p=1104031#post1104031

Thanks again for your respones!

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

*without a scheduling system

···

On 6/6/06, x1 <caldridge@gmail.com> wrote:

I would either fix cron or use
at(http://en.wikipedia.org/wiki/At_(Unix_command)\)

While all these options work, in a production environment I wouldnt
feel right with a scheduling system of some sort. autosys/cron/at etc.

On 6/6/06, Hunter <walkerhunter@gmail.com> wrote:
> Jeff Pritchard wrote:
> > Not to be contrarian, but pickaxe page 180 says that ARGV[0] is not the
> > name of the program (unlike in C). There's other stuff I don't get
> > about your snippet as well.
> > Why ["ruby" + ARGV]??? Seems like you're using + to add an array to a
> > string? Can you explain it for us newbies?
> >
> > Working from Pickaxe page 180 I would have come up with something like:
> > sleep 180 # obvious
> > pieces = [ruby, $0].concat(ARGV) # $0 is program name
> > exec pieces.join(' ') # to string and execute it
> >
> > BTW, many rubyist's would, I think, stick the .join(' ') on the end of
> > the array composition, like this:
> > exec [ruby, $0].concat(ARGV).join(' ')
> >
> > Is there a move away from such Perlish pursuits, or are you still
> > considered a "Sheila" if you split things like this up into more easily
> > understandable pieces?
> >
> > thanks,
> > jp
> >
> > unknown wrote:
> >> On Wed, 7 Jun 2006, Hunter Walker wrote:
> >>
> >>>
> >>> --
> >>> Posted via http://www.ruby-forum.com/\.
> >>
> >> sleep 180
> >> exec ["ruby" + ARGV].join(' ')
> >>
> >> -a
>
> I just got an error when I tried the code below:
>
> exec ["ruby" + ARGV].join(' ')
>
> I'll try the code you suggested, JP. Thanks!
>
> --
> Posted via http://www.ruby-forum.com/\.
>

Austin Ziegler wrote:

···

On 6/6/06, Hunter Walker <walkerhunter@gmail.com> wrote:

I agree 100%. I just want to get something up and running while I
figure out the cron issue.

Below is my thread on the subject in the Ubuntu forums if you have any
ideas:

http://ubuntuforums.org/showthread.php?p=1104031#post1104031

Thanks again for your respones!

What I usually do in cases like this is something like:

  * * * * * /path/to/bash
script-to-setup-environment-and-run-my-program.sh

It allows me significantly more control.

-austin

Hi Austin,

So I should create a shell script that runs the command? Can you
provide an example? Sorry, I am new at this...if you couldn't alreay
tell. :slight_smile:

Thanks!

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

The first trick is that you need to build the necessary $PATH and
other environment for your application. So I'd start with:

% env > runner.sh

I'd then add your script to the bottom:

% echo `which ruby` myscript.rb >> runner.sh

Then, edit runner.sh to get rid of environment variables you *know*
you don't need and pare down the ones that you do need to the minimum
level required to run myscript.rb with Ruby.

-austin

···

On 6/6/06, Hunter Walker <walkerhunter@gmail.com> wrote:

So I should create a shell script that runs the command? Can you
provide an example? Sorry, I am new at this...if you couldn't alreay
tell. :slight_smile:

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

use something like this:

   harp:~ > cat /home/ahoward/bin/shush

   #! /usr/bin/env ruby

   $VERBOSE = nil

   stdin = ARGV.first == "-" and ARGV.shift
   command = stdin ? stdin.read : ARGV.join(" ")

   buf =
     IO.popen("bash --login", "r+") do |pipe|
       puts "{ #{ command } ;} 2>&1"
       pipe.puts "{ #{ command } ;} 2>&1"
       pipe.close_write
       pipe.read
     end

   exitstatus = $?.exitstatus

   puts buf unless exitstatus == 0 or exitstatus == 42

   exit exitstatus

this script runs it's command line under a bash login shell - so you'll have
your entire 'normal' environment available. additionally it mops up any
output __unless__ the program fails - in which case it dumps it back out.
this is because cron emails it's user the output of any program - this way you
only get emails when programs fail.

regards.

-a

···

On Wed, 7 Jun 2006, Hunter Walker wrote:

So I should create a shell script that runs the command? Can you provide an
example? Sorry, I am new at this...if you couldn't alreay tell. :slight_smile:

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama