【问题标题】:crontab didn't run on ubuntu 14.04crontab 没有在 ubuntu 14.04 上运行
【发布时间】:2015-07-10 03:56:56
【问题描述】:
  • 我在 DigitalOcean VPS 上遇到了关于 set crontab Ubuntu 14.04 的问题。
  • 这是我的步骤:
    • 首先,创建新的crontab:crontab -e
    • 其次,我设置了这个命令:

这是我的 crontab,我将它设置为每天上午 10:26 运行:

26 10 * * * /bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=production bundle exec rake check:user >> log/cron_log.log'
  • 第三,我检查了 crontab 为crontab -l
  • 最后,我以sudo service cron restart 重新启动crontab

但它无论如何都没有运行(我等到 10:24 到 10:30),我复制了这个命令并在我的控制台中运行它,它运行得很好。

所以,我不明白为什么它的运行方式与 crontab 不同。希望大家能解释一下或者给我一些建议。非常感谢。

编辑:我尝试使用gem whenever,但它没有运行。

set :environment, "production"
env :PATH, ENV['PATH']
env :GEM_PATH, ENV['GEM_PATH']
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}

# Learn more: http://github.com/javan/whenever
every :day, at: '10:26am' do
  rake "check:user"
end

我更新了我的 crontab 使用,只要whenever -w 但它不起作用。

更新: @Coderhs:我尝试运行此命令bundle exec whenever --update-crontab RAILS_ENV=production,但它不起作用:(。

这是我使用命令 crontab -l 时的 crontab 列表:

# Begin Whenever generated tasks for: RAILS_ENV=production
PATH=/var/www/my_app/shared/bundle/ruby/2.2.0/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1@global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/rvm/bin
GEM_PATH=""

26 10 * * * /bin/bash -l -c 'cd /var/www/my_app/releases/20150710024713 && RAILS_ENV=production bundle exec rake user:check --silent >> log/cron_log.log 2>> log/cron_error_log.log'

# End Whenever generated tasks for: RAILS_ENV=production

它仍然没有运行。

更新: 问题解决了。因为在我当地的时间与服务器时间不同。 所以,我刚刚将服务器时间设置为与我的本地时间相同。谢谢大家支持我。

【问题讨论】:

  • 当您创建 crontab 时,请确保您是 admin/su。如果那不起作用,请尝试摆脱那些单个滴答声。不要认为你需要它们围绕你的命令
  • @FirebladeDan 是的,我使用了root 许可。但它没有用。

标签: ruby-on-rails crontab ubuntu-14.04


【解决方案1】:

如前所述,我会在任何时候使用 gem。

确保在每次 capistrano 部署后再次运行 -w 以查找正确的发布目录。

确保在 -w 时以与运行应用程序相同的用户身份运行。除非您的应用以 root 身份运行,否则不要使用 root,这是不应该的。

此外,您应该在 Capistrano 部署配置中将您的日志目录设置为共享目录。 config/deploy.rb 中的类似内容:

set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'pids', 'public/uploads', 'public/temp')

还可以在终端中运行“日期”命令,查看时间。这可能不是您的当地时间,因此 cron 可以工作,但与您预期的时间完全不同。

【讨论】:

    【解决方案2】:

    问题可能是因为运行 cron 的用户无法访问 ruby​​ 命令“bundle”。您将不得不使用完整的浴来捆绑 crontab 中的可执行文件。

    这个问题的更好的解决方案是使用when gem https://github.com/javan/whenever

    它将管理与 crontab 相关的所有内容,以避免此类问题。

    schedule.rb 随时

    set :environment, "production"
    set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
    env :PATH, ENV['PATH']
    env :GEM_PATH, ENV['GEM_PATH']
    
         # Learn more: github.com/javan/whenever
    every :day, at: '10:26am' do
      rake "check:user"
    end
    

    别忘了跑步。

    bundle exec whenever --update-crontab RAILS_ENV=production
    

    设置 shedule.rb 之后

    【讨论】:

    • 我尝试使用gem whenever,但它没有运行。这是我的代码: set :environment, "production" set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"} # 了解更多:github.com/javan/whenever every :一天,在:'10:26am' 做 rake "check:user" end
    • @KhanhPham,我已经更新了我的答案。你能试试这个,让我知道它是否运行。如果它仍然没有运行,你可以粘贴cront_error_log.log 的输出吗
    • 运行你的命令后,我检查了我的日志文件夹,它没有创建任何日志文件。
    • 您将 env :PATH 和 env :GEM_PATH 添加到 schedule.rb
    • 是的,我在 schedule.rb 中添加了 env::PATH 和 env :GEM_PATH。但是还是不行。
    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 2015-07-10
    • 2016-07-08
    • 1970-01-01
    • 2015-02-19
    • 2019-12-14
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多