使用 Capistrano 启动后台任务 [英] Starting background tasks with Capistrano

查看:22
本文介绍了使用 Capistrano 启动后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 RubyOnRails-App,我必须在 Capistrano 部署结束时启动后台作业.为此,我在 deploy.rb 中尝试了以下操作:

For my RubyOnRails-App I have to start a background job at the end of Capistrano deployment. For this, I tried the following in deploy.rb:

run "nohup #{current_path}/script/runner -e production 'Scheduler.start' &", :pty => true

有时这可行,但大多数时候它不会启动进程(= 未在 ps -aux 中列出).并且没有错误消息.而且没有nohup.out,不在home目录下也不在rails app目录下.

Sometimes this works, but most of the time it does not start the process (= not listed in ps -aux). And there are no error messages. And there is no nohup.out, not in the home directory and not in the rails app directory.

我尝试在 scheduler.rb 中使用 trap('SIGHUP', 'IGNORE') 代替 nohup,但结果是一样的.

I tried using trap('SIGHUP', 'IGNORE') in scheduler.rb instead of nohup, but the result is the same.

让它工作的唯一方法是删除 ":pty => true" 并在 "cap deploy" 结束时手动执行 Ctrl-C.但我不喜欢这样……

The only way to get it work is removing the ":pty => true" and do a manual Ctrl-C at the end of "cap deploy". But I don't like this...

还有其他机会调用这个 Scheduler.start 吗?或者获取更多错误信息?

Are there any other chances to invoke this Scheduler.start? Or to get some more error messages?

我在服务器上使用 Rails 2.3.2、Capistrano 2.5.8、Ubuntu Hardy

I'm using Rails 2.3.2, Capistrano 2.5.8, Ubuntu Hardy on the Server

推荐答案

如果 :pty => true,则(通常)不会加载用户 shell 启动脚本(例如 bashrc 等).由于缺少依赖环境变量,我的 ruby​​ 程序在启动后立即退出.

With :pty => true, user shell start-up scripts (e.g. bashrc, etc.) are (usually) not loaded. My ruby program exited right after launching because of the lack of dependent environment variables.

如果没有 :pty => true,正如您在问题中所述,capistrano 会挂在那里等待进程退出.您需要重定向 stdout 和 stderr 以使其立即返回.

Without :pty => true, as you described in the question, capistrano hangs there waiting for the process to exit. You'll need to redirect both stdout and stderr to make it return immediately.

run 'nohup ruby -e "sleep 5" &' # hangs for 5 seconds
run 'nohup ruby -e "sleep 5" > /dev/null &' # hangs for 5 seconds
run 'nohup ruby -e "sleep 5" > /dev/null 2>&1 &' # returns immediately. good.

如果您的后台任务仍然没有运行.尝试将 stdout 和 stderr 重定向到日志文件,以便您可以调查输出.

If your background task still doesn't run. Try redirecting stdout and stderr to a log file so that you can investigate the output.

这篇关于使用 Capistrano 启动后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆