# Rails, rake and crontab

**URL:** <https://www.ruby-forum.com/t/rails-rake-and-crontab/191505>\
**Category:** Rails\
**Created:** [July 13, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/rails-rake-and-crontab/191505 "2010-07-13T16:50:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [July 13, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/rails-rake-and-crontab/191505/1 "2010-07-13T16:50:04Z")

</div>

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/myapp && RAILS\_ENV=production  
/usr/local/bin/rake myraketask --trace \>\>  
/var/www/html/myapp/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`’  
It is noteworthy that the task is working fine if I run it manually.

Thanks,

M

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [July 13, 2010, 5:04pm UTC](https://www.ruby-forum.com/t/rails-rake-and-crontab/191505/2 "2010-07-13T17:04:47Z")

</div>

On 13 July 2010 15:50, med addame [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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/myapp && RAILS\_ENV=production  
> /usr/local/bin/rake myraketask --trace \>\>  
> /var/www/html/myapp/log/cron\_log.log 2\>&1

If you type exactly that command (from the cd obviously) into a  
terminal, does it work? Do you have permission to write into the log  
directory? Though can’t see why that would give that error.

> 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’

Are you sure this is as a result of the command above? It seems to be  
in the wrong directory.

What is in line 98 of boot.rb?

Colin

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [July 13, 2010, 5:12pm UTC](https://www.ruby-forum.com/t/rails-rake-and-crontab/191505/3 "2010-07-13T17:12:08Z")

</div>

the crontab entry was :

- 
  - 
    - 
      - cd /var/www/html/metajorn && RAILS\_ENV=production  
/usr/local/bin/rake myraketask --trace \>\>  
/var/www/html/metajorn/log/cron\_log.log 2\>&1

I changed the name to myapp just in the post … but omitted to change  
it in the error trace.

So, yes when run manually it works just fine and file permissions are  
set to 777.  
I think it is related to some environment information that are not  
loaded by cron … but I haven’t find yet what is exactly missing.

Colin L. wrote:

> On 13 July 2010 15:50, med addame [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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/myapp && RAILS\_ENV=production  
> > /usr/local/bin/rake myraketask --trace \>\>  
> > /var/www/html/myapp/log/cron\_log.log 2\>&1
> 
> If you type exactly that command (from the cd obviously) into a  
> terminal, does it work? Do you have permission to write into the log  
> directory? Though can’t see why that would give that error.
> 
> > 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’
> 
> Are you sure this is as a result of the command above? It seems to be  
> in the wrong directory.
> 
> What is in line 98 of boot.rb?
> 
> Colin

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [July 13, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/rails-rake-and-crontab/191505/4 "2010-07-13T17:29:51Z")

</div>

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.

med addame wrote:

> 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/myapp && RAILS\_ENV=production  
> /usr/local/bin/rake myraketask --trace \>\>  
> /var/www/html/myapp/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`’  
> It is noteworthy that the task is working fine if I run it manually.
> 
> Thanks,
> 
> M
