简体   繁体   English

如何在Amazon EC2上为Rails应用程序运行CRON作业?

[英]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)? 谁能给我一个提示,如何为在Amazon EC2(Ubuntu)上运行的Rails应用设置CRON作业?

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. 尝试创建一个模型方法来执行您的crondjob的任务。 You can execute the script by using rails runner. 您可以使用railsRunner执行脚本。

rails runner 'User.deliver_reminder_emails!'

The whenever gem can even do fancier stuff: https://github.com/javan/whenever 每当gem可以做更出色的事情时: https : //github.com/javan/whenever

You should have a look at rake tasks. 您应该看一下rake任务。 Check this , this and this . 检查这个这个这个

Running them is as simple as 运行它们就像

rake foo:bar

You can schedule the same in your crontab ; 您可以在crontab安排相同的时间; You might need to setup the correct PATH variable in case you are managing gems locally using rvm and bundler . 如果要使用rvmbundler在本地管理gem,则可能需要设置正确的PATH变量。

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. 这对我来说相当棘手,因为ELB-EC2实例试图以root身份运行cron作业,这似乎导致一组不同的权限,捆绑等。

It's a bit hacky, but this worked: 这有点hacky,但这行得通:

 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. 基本上,su是通常运行rails的用户,并显式说明bundle的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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