Ruby-on-rails Cron 作业在 When gem 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7548730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:02:42  来源:igfitidea点击:

Cron job not working in Whenever gem

ruby-on-railsrubycronwhenever

提问by Grant David Bachman

I have an application that contains a bunch of tasks, and every day I want to run a cron job that creates a DayTask for each Task in the database. A Task has_many DayTasks and these daytasks are what users will be checking off every day. I'm using the whenever gem but it doesn't seem to be running at all. Any ideas?

我有一个包含一堆任务的应用程序,每天我都想运行一个 cron 作业,为数据库中的每个任务创建一个 DayTask。一个 Task has_many DayTasks,这些 daytasks 是用户每天要检查的。我正在使用每当 gem 但它似乎根本没有运行。有任何想法吗?

config/schedule.rb

配置/计划.rb

every 1.day, :at => "12:01am" do
  runner "Task.generate_tasks_for_day"  
end

Task.rb

任务文件

  def generate_tasks_for_day
    Task.all.each do |task|
      task.day_tasks.create(:target_date => Date.today)
    end 
  end 

result of running the 'whenever command'

运行“whenever 命令”的结果

1 0 * * * /bin/bash -l -c 'cd /home/grant/rails_projects/GoalTwist && script/rails runner -e production '\''Task.generate_tasks_for_day'\'''

Note: I've been changing the times in config/schedule.rb every time I want to test run it.

注意:每次我想测试运行它时,我一直在更改 config/schedule.rb 中的时间。

回答by damoiser

Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).

最后,我已经解决了如何在任何时候运行 gem。它在生产中运行良好,但在开发模式下运行良好(我认为要在开发模式下运行良好,您必须做一些技巧)。

Then, these are the processes to do:

然后,这些是要执行的过程:

  1. install the gem
  2. write your scheduler.rbfile
  3. push to the remote server
  4. login to the remote server (for example with ssh)
  5. see if whenever is good uploaded by running in terminal: whenever
  6. update whenever crontab by running: whenever --update-crontab
  7. restart the server crontab (for example in Ubuntu server): sudo service cron restart
  8. check if crontab is good implemented on the server: crontab -l
  1. 安装宝石
  2. 写你的scheduler.rb文件
  3. 推送到远程服务器
  4. 登录到远程服务器(例如使用ssh
  5. 通过在终端中运行查看何时上传好: whenever
  6. 通过运行在 crontab 时更新: whenever --update-crontab
  7. 重新启动服务器 crontab(例如在 Ubuntu 服务器中): sudo service cron restart
  8. 检查 crontab 是否在服务器上实现良好: crontab -l

That's it!

就是这样!

Personally, I prefer to set up my crons directly from the server:

就个人而言,我更喜欢直接从服务器设置我的 cron:

  1. Edit the crontab: crontab -e
  2. Append my cron (e.g. every day at 5:00 AM - can be little different for not-Linux-based server):
    0 5 * * * /bin/bash -l -c 'cd /path_to_my_app/current && RAILS_ENV=production bundle exec rake my_cron_rake'
  3. Check if good implemented: crontab -l
  4. Done
  1. 编辑 crontab: crontab -e
  2. 附加我的 cron(例如,每天早上 5:00 - 对于非基于 Linux 的服务器可能略有不同):
    0 5 * * * /bin/bash -l -c 'cd /path_to_my_app/current && RAILS_ENV=production bundle exec rake my_cron_rake'
  3. 检查是否良好实施: crontab -l
  4. 完毕

回答by Graham T

You have to actually register the job with the crontab by running:

您必须通过运行以下命令实际向 crontab 注册作业:

whenever --update-crontab

whenever --update-crontab

Also if you're trying to get jobs running locally, add :environment => "development"to your task

此外,如果您尝试在本地运行作业,请添加 :environment => "development"到您的任务中

runner "MyTask.some_action", :environment => "development"

runner "MyTask.some_action", :environment => "development"

回答by Dende

if whenevercommand gives you -bash: whenever: command not found, try bundle exec whenever

如果whenever命令给你-bash: whenever: command not found,试试bundle exec whenever

回答by Hugo

Try to execute the whenever generated command directly from terminal or add the following line [email protected]to your crontab.

尝试直接从终端执行每当生成的命令或[email protected]将以下行添加 到您的 crontab。