如何设置 cron 环境来运行 Ruby 脚本?
我在 Linode 虚拟机上有一个 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
执行'", "/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:in
get'", "/var /lib/gems/1.8/gems/httparty-0.7.3/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
感谢您的帮助!
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:in
perform'", "/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:in
get'", "/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 that 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 cron 运行脚本时,它不会继承您的环境。通常,最简单的事情是编写一个包装器 shell 脚本,该脚本可以获取您需要的任何内容,然后调用您的 ruby 脚本。
例如,我的大多数 cron 脚本看起来都是这样的
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