简体   繁体   English

在Ruby脚本中需要Ruby Gem会破坏Cron作业执行

[英]Requiring a Ruby Gem in Ruby Script breaks Cron Job Execution

I'm trying to run a cron job using Gems. 我正在尝试使用Gems运行一个cron作业。 I've installed ruby via RVM and when I require a gem it breaks the cron job. 我已经通过RVM安装了ruby,当我需要一个gem时它打破了cron的工作。 I've tried requiring two totally different gems, PG / Pry, and when I require either, the cronjob doesn't complete. 我试过要求两个完全不同的宝石,PG / Pry,当我需要时,cronjob没有完成。 Here is the "testing code" that works fine: 这是正常工作的“测试代码”:

open('/home/log.log', 'a') do |f|
  f.puts Time.now.to_s
end

Here is how I setup the cronjob: 以下是我设置cronjob的方法:

* * * * * /usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/test1.rb

I can see new output every minute. 我每分钟都能看到新的输出。 And when I add a require gem line at the top, it then breaks, but only when run through cron: 当我在顶部添加一个require gem行时,它会中断,但只有在通过cron运行时:

require 'pg'

open('/home/log.log', 'a') do |f|
  f.puts Time.now.to_s
end

The cronjob runs (I can see it execute in the sys log), but never completes (no output ever makes it into the text file). cronjob运行(我可以看到它在sys日志中执行),但从未完成(没有输出进入文本文件)。 I've tried this on two separate servers one Debian, one CentOS, and both have the same issue. 我在两个独立的服务器上尝试过这个,一个是Debian,一个是CentOS,两者都有同样的问题。 Oddly enough this only affects the cron job, if I run the same ruby file from console: /home/test1.rb it will work just fine. 奇怪的是,这只会影响cron作业,如果我从控制台运行相同的ruby文件:/home/test1.rb它会正常工作。

Any help would be great. 任何帮助都会很棒。

You need to setup your crontab with rvm eg: 您需要使用rvm设置crontab,例如:

rvm cron setup rvm cron设置

With that rvm sets your environment variables in your crontab file 使用该rvm在crontab文件中设置环境变量

then you have a crontab file having this at the top: 然后你有一个crontab文件在顶部:

PATH="/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/rvm/gems/ruby-1.9.3-p194@global/"
rvm_env_string='ruby-1.9.3-p194'
rvm_path='/usr/local/rvm'
rvm_ruby_string='ruby-1.9.3-p194'
RUBY_VERSION='ruby-1.9.3-p194'
GEM_HOME='/usr/local/rvm/gems/ruby-1.9.3-p194'
GEM_PATH='/usr/local/rvm/gems/ruby-1.9.3-p194:/usr/local/rvm/gems/ruby-1.9.3-p194@global'
MY_RUBY_HOME='/usr/local/rvm/rubies/ruby-1.9.3-p194'
IRBRC='/usr/local/rvm/rubies/ruby-1.9.3-p194/.irbrc'

Then you can stick your crontask beneath it 然后你可以把你的crontask粘在它下面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM