简体   繁体   中英

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

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM