rubyon Rails、rake、crontab 和编码

发布于 2024-09-08 22:40:21 字数 1702 浏览 0 评论 0原文

我有一个运行良好的 Rails rake 任务。我希望 crontab 定期运行此任务,因此我将其添加到 crontab 中,如下所示:

0,30,0 * * * * cd /var/www/html/metajorn && RAILS_ENV=production /usr/local/bin/rake myraketask --trace   >> /var/www/html/metajorn/log/cron_log.log 2>&1

在 cron_log.log 中,我看到以下错误:

rake aborted!
invalid byte sequence in US-ASCII
/var/www/html/metajorn/config/boot.rb:98:in `parse_gem_version'
/var/www/html/metajorn/config/boot.rb:80:in `gem_version'
/var/www/html/metajorn/config/boot.rb:59:in `load_rails_gem'
/var/www/html/metajorn/config/boot.rb:54:in `load_initializer'
/var/www/html/metajorn/config/boot.rb:38:in `run'
/var/www/html/metajorn/config/boot.rb:11:in `boot!'
/var/www/html/metajorn/config/boot.rb:110:in `<top (required)>'
/var/www/html/metajorn/Rakefile:4:in `require'
/var/www/html/metajorn/Rakefile:4:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2383:in        `raw_load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2017:in `block in load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2000:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/usr/local/bin/rake:31:in `<main>'

值得注意的是,如果我手动运行该任务,则该任务工作正常。

谢谢,

I have a rails rake task which runs just fine. I want this task to be run periodically by crontab so I added it to crontab as follows :

0,30,0 * * * * cd /var/www/html/metajorn && RAILS_ENV=production /usr/local/bin/rake myraketask --trace   >> /var/www/html/metajorn/log/cron_log.log 2>&1

In cron_log.log I'm seeing the following error:

rake aborted!
invalid byte sequence in US-ASCII
/var/www/html/metajorn/config/boot.rb:98:in `parse_gem_version'
/var/www/html/metajorn/config/boot.rb:80:in `gem_version'
/var/www/html/metajorn/config/boot.rb:59:in `load_rails_gem'
/var/www/html/metajorn/config/boot.rb:54:in `load_initializer'
/var/www/html/metajorn/config/boot.rb:38:in `run'
/var/www/html/metajorn/config/boot.rb:11:in `boot!'
/var/www/html/metajorn/config/boot.rb:110:in `<top (required)>'
/var/www/html/metajorn/Rakefile:4:in `require'
/var/www/html/metajorn/Rakefile:4:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2383:in        `raw_load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2017:in `block in load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2000:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/usr/local/bin/rake:31:in `<main>'

It is noteworthy that the task is working fine if I run it manually.

Thanks,

M

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

迷路的信 2024-09-15 22:40:21

感谢卡尔·斯莫特里兹的提示!

我终于解决了这个问题:
cron 使用的环境变量可能与用户环境变量不同...对我来说就是这样,我的 crond 服务使用 us-ascii 编码,而我的 rake 任务使用 utf-8。

要解决这个问题,我只需通过将以下行添加到我的 crond 文件(在我的 centos5 中,它位于: /var/spool/cron/root )来更改 crond 的编码,

SHELL=/bin/bash
LANG=en_US.UTF-8
LANGUAGE=en
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"

然后重新启动 crond : service crond restart

M 。

Thanks Carl Smotricz for the hint !

I finally solved the problem:
cron uses environment variables that may be different from the user environment variables... it was the case for me my crond service uses us-ascii encoding while my rake task uses utf-8.

To fix the issue, I just had to change the encoding of crond by adding the following lines to my crond file (in my centos5 it is locaed in: /var/spool/cron/root )

SHELL=/bin/bash
LANG=en_US.UTF-8
LANGUAGE=en
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"

Than, restarted crond : service crond restart

M.

爱的故事 2024-09-15 22:40:21

我只能猜测,但请考虑 cron 只为您的任务提供了环境的最小子集;如果我没记错的话,仅限 USERSHELL 之类的东西。从您的用户帐户运行命令,您将拥有一个使用各种值初始化的环境 - 使用 set 来看看!您的 PATH 通常有很多 cron 环境中缺少的内容,但还有很多其他可能性。

最常见的是,手动运行和 cron 运行之间的差异是由环境差异造成的,如上所述。

I can only guess, but please consider that cron provides your task with only a minimal subset of the environment; something like USER and SHELL only, if I remember correctly. Running a command from your user account, you have an environment initialized with all kinds of values - use set to take a look! Your PATH often has a lot of stuff that's missing in the cron environment, but there are lots of other possibilities.

Most commonly, differences between hand-running and cron-running result from differences in the environment, as described.

甜宝宝 2024-09-15 22:40:21

是的,由于某种原因,当 cron 运行 rake 时,它​​在从environment.rb 加载 RAILS_GEM_VERSION 时偶然发现了 Ruby 1.9 字符编码问题,

#boot.rb
  def gem_version
    if defined? RAILS_GEM_VERSION
      RAILS_GEM_VERSION
    elsif ENV.include?('RAILS_GEM_VERSION')
      ENV['RAILS_GEM_VERSION']
    else
      parse_gem_version(read_environment_rb)
    end
  end

如何尝试通过在 cron rake 命令中定义 RAILS_GEM_VERSION 来绕过这个问题?

RAILS_ENV=production RAILS_GEM_VERSION=2.3.5 /usr/local/bin/rake myraketask 

Yes, for some reason when cron runs rake, it stumbles on the Ruby 1.9 character encoding issues when loading the RAILS_GEM_VERSION from environment.rb

#boot.rb
  def gem_version
    if defined? RAILS_GEM_VERSION
      RAILS_GEM_VERSION
    elsif ENV.include?('RAILS_GEM_VERSION')
      ENV['RAILS_GEM_VERSION']
    else
      parse_gem_version(read_environment_rb)
    end
  end

how about trying to bypass this by defining the RAILS_GEM_VERSION in the cron rake command?

RAILS_ENV=production RAILS_GEM_VERSION=2.3.5 /usr/local/bin/rake myraketask 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文