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.
-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.
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
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.
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.
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.
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?
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?
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?