开发者

How do I set up the cron environment to run a Ruby script?

I have a Ruby script on a Linode VM that I'd like to run every minute.

When I run the ruby script manually via the command line, it runs perfectly. But when the cron runs the file, it throws a strange error:

undefined method `closed?' for nil:NilClass

["/usr/lib/ruby/1.8/net/http.rb:1060:in request'", "/var/lib/gems/1.8/gems/httparty-0.7.3/lib/httparty/request.rb:69:inperform'", "/var/lib/gems/1.8/gems/httparty-0.7.3/lib/httparty.rb:394:in perform_request'", "/var/lib/gems/1.8/gems/httparty-0.7.3/lib/httparty.rb:346:inget'", "/var/lib/gems/1.8/gems/httparty-0.7.3/lib/httparty.rb:426

I've been investigating this problem and it seems as if it has something to do with the stripped down environment tha开发者_开发知识库t cron runs the script in.

Do I need to source my Ruby environment in the crontab file? If so, what exactly do I need?

Here's my env:

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.5
  - RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
  - INSTALLATION DIRECTORY: /var/lib/gems/1.8
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /var/lib/gems/1.8/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /var/lib/gems/1.8
     - /root/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://gems.rubyforge.org/

which ruby: /usr/bin/ruby

Thanks for the help!


When cron runs scripts, it does not inherit your environment. Usually, the easiest thing to do is write a wrapper shell script that sources whatever you need and then calls your ruby script.

For example, most of my cron scripts look something like

#!/bin/sh

. ${HOME}/.bashrc

# Do Stuff - here is where you would call your ruby script
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜