本文介绍了在 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 作业执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:59
查看更多