user984621
user984621

Reputation: 48443

How do I run CRON jobs for a Rails app on Amazon EC2?

Could anyone give me a tip how to set up CRON jobs for a Rails app that's running on Amazon EC2 (Ubuntu)?

Trying to find some tutorials or tips how to make it work, but still without any success. Thank you

Upvotes: 1

Views: 1156

Answers (3)

ericpeters0n
ericpeters0n

Reputation: 668

This worked out to be rather tricky for me, as the ELB-EC2 instance was trying to run a cron job as root, which seemed to result in a different set of permissions, bundle, etc.

It's a bit hacky, but this worked:

 sudo su ec2-user bin/bash -lc "cd /var/app/current && /opt/rubies/ruby-2.1.8/bin/bundle exec/bundle exec rails runner -e your_env \"Model.do_something\""

Basically, su to the user that usually runs rails, and explicitly state the paths for bundle.

Upvotes: 0

Anshul Goyal
Anshul Goyal

Reputation: 76827

You should have a look at rake tasks. Check this, this and this.

Running them is as simple as

rake foo:bar

You can schedule the same in your crontab; You might need to setup the correct PATH variable in case you are managing gems locally using rvm and bundler.

Upvotes: 0

jewilmeer
jewilmeer

Reputation: 1936

Try creating a model method which execute a task for your crondjob. You can execute the script by using rails runner.

rails runner 'User.deliver_reminder_emails!'

The whenever gem can even do fancier stuff: https://github.com/javan/whenever

Upvotes: 1

Related Questions