【问题标题】:How to run a task rake on ruby on rails with a crontab ? (which works manually, without crontab)如何使用 crontab 在 ruby​​ on rails 上运行任务 rake? (手动工作,没有 crontab)
【发布时间】:2012-07-31 01:46:05
【问题描述】:

我在 aws 微型服务器上部署(使用 capistrano)一个 ruby​​ on rails 项目。 我在 ruby​​ 1.9.2-290 和 rails 3.2.6 上,我也使用 bundler。 我在我的 opt/rails-project/lib/tasks/tasks.rake 中开发了一个任务 rake

namespace :myclass do
    task "my-task" => :environment do
        # do the stuff which work nicely if i enter my command line manually
    end
end

这就是我在 crontab 中的称呼:

*/3 * * * * cd /opt/rails-project/current && /opt/rails-project/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake myclass:my-task RAILS_ENV=production >> ~/logs-my-task.txt

文件 ~/logs-my-task.txt 每隔 3 分钟创建和更新一次。此文件仅包含 capistrano 的版本发布信息,但没有来自我的任务 rake。

正如我在任务 rake 的评论中所说,如果我通过 ssh 直接在服务器中启动此命令,我的任务 rake 就可以完成它的工作......

我整天/晚上都在网上搜索,但无法弄清楚。

我试图从 rails 中删除 http_basic auth 但同样的问题。

希望你有一个想法, 感谢您的帮助!

【问题讨论】:

标签: ruby-on-rails ruby debugging rake crontab


【解决方案1】:

试着把这部分放在

cd /opt/rails-project/current && /opt/rails-project/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake myclass:my-task RAILS_ENV=production >> ~/logs-my-task.txt

在某个文件中,somescript.sh,赋予执行权限:

chmod +x somescript.sh

并尝试手动运行它:

/path/to/somescript.sh

如果可以的话,试试放到crontab里面:

*/3 * * * * /path/to/somescript.sh

将复杂的内容放入脚本中以从 crontab 运行通常会有所帮助。

下一步,确保您的 PATH 环境变量与您的 shell 和 cron 相同。您可以在 crontab 或脚本中设置它。

【讨论】:

  • 谢谢!我不需要使用 sheel 脚本,但是当我手动启动它时它可以帮助我弄清楚。
【解决方案2】:

使用denis.peplin 推荐的shell 脚本并手动启动后,我遇到了这里描述的问题:Ruby on Rails and Rake problems: uninitialized constant Rake::DSL

我在我的 Rakefile 中包含以下行并让我的 crontab 保持原来的状态:

require 'rake/dsl_definition'

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2013-12-20
    • 2011-06-22
    相关资源
    最近更新 更多