2

I did some researching but all cron and bundle exec related didn't cover the problem I am having so excuse me again if this had been discussed.

I am running Ubuntu 13.10 and have a Ruby On Rails app that have few rake tasks that needs to be run on Cron every few minutes or so.

I run a whenever gem, with the help of which, this syntax

every 3.minutes do
  rake 'update_balance'
end

transforms to this line in crontab file

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'

And when I copy this line exactly

/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'

and run in it console, it runs perfectly fine and updates several records in database, as supposed.

But when set to cron, I can see it running in /var/log/syslog file, but nothing is really executed.

May 13 13:06:01 sandbox2 CRON[9656]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
May 13 13:06:01 sandbox2 CRON[9655]: (CRON) info (No MTA installed, discarding output)
May 13 13:09:01 sandbox2 CRON[9789]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')

Even when I add &>/tmp/mycommand.log to crontab command, every next launch of cron command just completely truncates this file, however again, If I launch it manually, it works nicely and leaves me with this output.

2014-05-13T11:11:25Z 10292 TID-2asbo INFO: Sidekiq client with redis options  {:url=>"redis://127.0.0.1"}
Sent task for updating 2 users

Any help with this issue is much appreciated. Thanks.

Vitali
  • 690
  • 9
  • 29

1 Answers1

5

I've encountered problems like this before due to the fact that cron runs as a different user. With rake in particular, i have to use the full path to rake since the cron user doesn't have the right folder in it's PATH.

So, my cron lines for rake tasks look like this:

30 8 * * 1 cd /ebs/www/apps/myproject/www && /usr/local/bin/rake mailer:send_weekly_expiring_users_reminder RAILS_ENV=production
Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • 2
    You can also [set environment variables in the `crontab` itself](http://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use) if you want them to apply to all tasks. – tadman May 13 '14 at 15:31
  • that sounds more sensible :) – Max Williams May 13 '14 at 15:35
  • 2
    Thanks, it worked. Il look into environment variables later. This is how my crontab line looks by now. 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production /usr/local/bin/bundle exec /usr/bin/rake update_balance --silent' – Vitali May 13 '14 at 18:45
  • This also helped me a lot, everyone experiencing same issues should read this: http://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths – Vitali May 14 '14 at 07:10