在 Ruby 脚本中要求 Ruby Gem 会中断 Cron 作业执行 [英] Requiring a Ruby Gem in Ruby Script breaks Cron Job Execution

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

问题描述

我正在尝试使用 Gems 运行 cron 作业.我已经通过 RVM 安装了 ruby​​,当我需要 gem 时,它会破坏 cron 作业.我试过需要两个完全不同的 gem,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 的方法:

Here is how I setup the 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.

任何帮助都会很棒.

推荐答案

你需要用 rvm 设置你的 crontab 例如:

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

rvm cron 设置

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天全站免登陆