views:

46

answers:

3

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

A: 

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.

Carl Smotricz
A: 

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 
digiberber
A: 

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.

massinissa