【问题标题】:Cron job doesn't work with whenever gem每当 gem 时,Cron 作业都不起作用
【发布时间】:2015-10-02 13:12:43
【问题描述】:

我想创建一个计划 cron 作业。 我什么时候都用过。我的 Rails 版本是 4.1.6,我在 osx 中。

所以我创建了一个计划 cron 作业来测试。

schedule.rb

every 1.minutes do
    runner "DatabaseWatcher.run"
end

补丁:

/lib/DatabaseWatcher.rb

类:

class DatabaseWatcher
        def run
            puts "test"
        end
     end

在这之后我做了:

$ 任何时候

## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

$ 每当 -w

[write] crontab file written

我从未在命令行中看到“测试”。 我做错了什么?

【问题讨论】:

  • 它不是通过你的终端会话运行的,所以它不会在那里输出。
  • crontab -l的输出更新你的问题
  • # 每当生成任务时开始: /Users/diogo/MIT/config/schedule.rb * * * * * /bin/bash -l -c 'ls' # 每当生成任务时结束: /Users/diogo/MIT/config/schedule.rb
  • crontab 里面没有你的任务,所以我不会运行,你需要弄清楚为什么它没有写入 crontab
  • @Zahid 粘贴错误。这是最后一个版本: # Begin 每当生成任务时: /Users/diogo/MIT/config/schedule.rb * * * * * /bin/bash -l -c 'cd /Users/diogofrazao/MIT && bin/rails runner -e production '\''DatabaseWatcher.run'\''' # 每次生成任务时结束:/Users/diogo/MIT/config/schedule.rb

标签: ruby-on-rails ruby cron whenever


【解决方案1】:

我之前已经遇到过这个错误。 使用.self 在你的运行方法中添加类方法。如果 /lib/DatabaseWatcher.rb 中的 DatabaseWatcher 没有调用,则必须移入文件夹 app/models/database_watcher.rb

class DatabaseWatcher
  def self.run
    puts "test"
  end
end

这是为了在环境开发中更新你的 crontab。

# to update crontab
whenever --update-crontab --set environment=development
# show crontab list
crontab -l

供参考:

  1. rails-using-whenever-gem-in-development

【讨论】:

  • 他说了什么。尝试从 rails 控制台运行您的命令:DatabaseWatcher.run。它会给出一个错误。这不是正确的 ruby​​ 语法。你需要把它变成一个类方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2011-11-24
  • 1970-01-01
  • 2013-06-21
  • 2018-06-19
  • 1970-01-01
相关资源
最近更新 更多