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

发布于 2024-10-15 04:50:43 字数 1326 浏览 2 评论 0原文

我在 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:inget'", "/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: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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-10-22 04:50:43

当 cron 运行脚本时,它不会继承您的环境。通常,最简单的事情是编写一个包装器 shell 脚本,该脚本可以获取您需要的任何内容,然后调用您的 ruby​​ 脚本。

例如,我的大多数 cron 脚本看起来都是这样的

#!/bin/sh

. ${HOME}/.bashrc

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文