Question

How can I run a rake task in my production environment?

I have set up a rake task that I want to run once an hour using a cron expression. I got it working on my local dev environment, but I can’t get it to work on my production environment hosted here.

I have the following code in /lib/tasks/cron.rake:

task :cron => :environment do
  puts "starting job"
  Schedule.run_schedules
  puts "finished job"
end

The command I run on production:

rake cron RAILS_ENV=production

Expected output:

starting job
finished job

Actual output:

starting job
  Schedule Load (1.0ms) SELECT "schedules".* FROM "schedules" WHERE (next_occurrence_utc ,+ '2019-06-04 10:43:04.268746')
Rake aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied for relation schedules
: SELECT "schedules".* FROM "schedules" WHERE (next_occurrence_utc ,+ '2019-06-04 10:43:04.268746')

I tried running the task as the user that owns the database, but then I get this message:

rake aborted!
LoadError: cennot load such file -- bundler.setup

I tried installing bundler, but then I get an error saying that I don’t have permissions to do so.

I’m lost here, so any advice would be greatly appreciated.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Brian Hogan
DigitalOcean Employee
DigitalOcean Employee badge
June 26, 2019
Accepted Answer

HI! Let me see if I can help.

The first thing that comes to mind is that you do need to set the environment and use bundle. Be sure you’re in the root of your rails app, and that you’re using the user your app runs as. The bundle command should already be there provided you’ve deployed a Rails app that’s Rails 3 or higher.

cd YOUR_RAILS_APP_ROOT
RAILS_ENV=production bundle exec rake cron

Now, this doesn’t directly address your “how do I make it work” issue, but I strongly recommend using https://github.com/javan/whenever for things like this. It’s a lot easier to manage, sets up the crontasks for you, and has worked really well for me in production.

Hope that gets you somewhere.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.