rubyon Rails、rake、crontab 和编码
我有一个运行良好的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢卡尔·斯莫特里兹的提示!
我终于解决了这个问题:
cron 使用的环境变量可能与用户环境变量不同...对我来说就是这样,我的 crond 服务使用 us-ascii 编码,而我的 rake 任务使用 utf-8。
要解决这个问题,我只需通过将以下行添加到我的 crond 文件(在我的 centos5 中,它位于: /var/spool/cron/root )来更改 crond 的编码,
然后重新启动 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 )
Than, restarted crond : service crond restart
M.
你可能想看看这个:)
http://www.logikdev.com/ 2010/02/02/您的 cron-job 的区域设置/
You might want to take a look at this :)
http://www.logikdev.com/2010/02/02/locale-settings-for-your-cron-job/
我只能猜测,但请考虑 cron 只为您的任务提供了环境的最小子集;如果我没记错的话,仅限
USER
和SHELL
之类的东西。从您的用户帐户运行命令,您将拥有一个使用各种值初始化的环境 - 使用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 likeUSER
andSHELL
only, if I remember correctly. Running a command from your user account, you have an environment initialized with all kinds of values - useset
to take a look! YourPATH
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.
是的,由于某种原因,当 cron 运行 rake 时,它在从environment.rb 加载 RAILS_GEM_VERSION 时偶然发现了 Ruby 1.9 字符编码问题,
如何尝试通过在 cron rake 命令中定义 RAILS_GEM_VERSION 来绕过这个问题?
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
how about trying to bypass this by defining the RAILS_GEM_VERSION in the cron rake command?