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


#1

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


#2

On Wed, 7 Jun 2006, Hunter W. wrote:


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

sleep 180
exec [“ruby” + ARGV].join(’ ')

-a


#3

removed_email_address@domain.invalid wrote:

-Hunter


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

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


#4

On Wed, 7 Jun 2006, Daniel B. wrote:

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

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

-a


#5

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!


#6

On 6/6/06, Hunter W. removed_email_address@domain.invalid 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


#7

On 6/6/06, Hunter W. removed_email_address@domain.invalid 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:

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


#8

On Wed, 7 Jun 2006, Hunter W. 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:

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


#9

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 W. wrote:


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

sleep 180
exec [“ruby” + ARGV].join(’ ')

-a


#10

Jeff P. 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 W. 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!


#11

Austin Z. wrote:

On 6/6/06, Hunter W. removed_email_address@domain.invalid 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!


#12

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.


#13

On Wed, 7 Jun 2006, Jeff P. 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?

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


#14

Hi,

At Wed, 7 Jun 2006 11:22:00 +0900,
Jeff P. 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.


#15

unknown wrote:

Hi,

At Wed, 7 Jun 2006 11:22:00 +0900,
Jeff P. 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.


#16

*without a scheduling system