如何设置cron环境来运行Ruby脚本?

时间:2011-01-28 16:27:42

标签: ruby cron

我在Linode VM上有一个Ruby脚本,我想每分钟运行一次。

当我通过命令行手动运行ruby脚本时,它运行完美。但是当cron运行该文件时,它会抛出一个奇怪的错误:

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:in perform'”,“/ var / lib / gems / 1.8 / gems / httparty-0.7.7 / lib / httparty.rb:394:在perform_request'", "/var/lib/gems/1.8/gems/httparty-0.7.3/lib/httparty.rb:346:in获取'“,”/ var / lib / gems / 1.8 / gems / httparty-0.7.7 / lib / httparty.rb:426

我一直在调查这个问题,似乎它与cron运行脚本的剥离环境有关。

我是否需要在crontab文件中获取Ruby环境?如果是这样,我到底需要什么?

这是我的环境:

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

当cron运行脚本时,它不会继承您的环境。通常,最简单的方法是编写一个包装shell脚本,根据需要提供所需内容,然后调用ruby脚本。

例如,我的大多数cron脚本看起来像

#!/bin/sh

. ${HOME}/.bashrc

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