在Ruby脚本中需要Ruby Gem打破Cron作业执行 [英] Requiring a Ruby Gem in Ruby Script breaks Cron Job Execution

查看:250
本文介绍了在Ruby脚本中需要Ruby Gem打破Cron作业执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Gems运行cron作业。我已经通过RVM安装了ruby,当我需要一个gem打破了cron的工作。我试过要求两个完全不同的宝石,PG / Pry,当我要求,cronjob没有完成。这里是测试代码,工作正常:

I'm trying to run a cron job using Gems. I've installed ruby via RVM and when I require a gem it breaks the cron job. I've tried requiring two totally different gems, PG / Pry, and when I require either, the cronjob doesn't complete. Here is the "testing code" that works fine:

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

设置cronjob:

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

我每分钟都可以看到新的输出。当我在顶部添加一个require gem行时,它会断开,但只有在通过cron运行时:

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 'pg'

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

cronjob运行我可以看到它在sys日志中执行,但永远不会完成(没有输出,使其进入文本文件)。我试过这两个单独的服务器一个Debian,一个CentOS,和两个有同样的问题。奇怪的是,这只影响cron工作,如果我从控制台运行相同的ruby文件:/home/test1.rb它会工作正常。

The cronjob runs (I can see it execute in the sys log), but never completes (no output ever makes it into the text file). I've tried this on two separate servers one Debian, one CentOS, and both have the same issue. 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.

任何帮助将是巨大的。

Any help would be great.

推荐答案

您需要使用rvm设置crontab,例如:

You need to setup your crontab with rvm e.g:

rvm cron setup

rvm cron setup

使用rvm在crontab文件中设置环境变量

With that rvm sets your environment variables in your crontab file

那么你有一个crontab文件

then you have a crontab file having this at the top:

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'

然后你可以把你的crontask放在它下面

Then you can stick your crontask beneath it

这篇关于在Ruby脚本中需要Ruby Gem打破Cron作业执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆