7
votes

Stack:

Apache2

Rails 2.3.8

RedHat Linux

Ruby Enterprise 1.8.7

Got the following rake task in my app user's crontab which is meant to pull records into a database table every 15 min:

*/15 * * * * app_user cd /var/www/apps/my_app/current/ && rake thing:do_stuff RAILS_ENV=production

I can see that the cron daemon is running this task in the cron log, but the database table it's supposed to pull records into doesn't change. This task is working without error when I run it manually in the /var/www/apps/my_app/current directory, and pulls records into the table as I expect it to.

I reset the PATH variable in the crontab to reflect using REE, thinking maybe the default path wouldn't jive with /opt/ruby-enterprise...

How do I get this rake task to actually run with cron?

2
Are you running it as that user too? sudo su app_user, then run the command EXACTLY as it is in cron job. It should work in cron if it works like that. - Michael Papile

2 Answers

10
votes
0,15,30,45 * * * * /bin/bash -l -c 'cd /var/www/apps/my_app/current && RAILS_ENV=production bundle exec rake thing:do_stuff --silent'
8
votes

Try to use full path to rake binary (run in console which rake and replace rake with full path).

For example, if which rake returns the following path:

/Users/bob/.rvm/gems/ruby-1.9.3-p194/bin/rake

You should use the following command to run the rake task:

/Users/bob/.rvm/bin/rvm all do bundle exec rake allocator:snapshot

and I prefer whenever gem for cron jobs in ruby

How to detect if task failed in cron? On fail cron tries to send email. So you can configure postfix to use your smtp settings (from google for example), and add file ~/.forward containing only your email to home directory of user who is running that cronjob in your system.