部署 请问 capistrano 如何执行 Rails 项目 bin 目录下的可执行文件

hww · 2017年09月24日 · 最后由 hww 回复于 2017年09月24日 · 2215 次阅读

问题

使用 capistrano 部署 Rails 项目时遇到一个问题:运行 bin 目录下的文件,但是进程没起来。

配置文件及部署

deploy.rb 文件中新增了如下内容

on roles(:chasqui) do
  execute "for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done"

  execute <<-BASH.squish!
    cd #{deploy_to}/current
    && nohup ruby ./bin/chasqui_start >/dev/null 2>&1 &
  BASH
end

然后部署时相关结果如下:

...
INFO [397a5432] Running for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done as deploy@server_ip_a
DEBUG [397a5432] Command: for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done
INFO [aa096778] Running cd /home/deploy/project_name/current && nohup ruby ./bin/chasqui_start >/dev/null 2>&1 & as deploy@server_ip_a
DEBUG [aa096778] Command: cd /home/deploy/project_name/current && nohup ruby ./bin/chasqui_start >/dev/null 2>&1 &
INFO [aa096778] Finished in 0.065 seconds with exit status 0 (successful).
...

异常

部署成功之后,我上去对应服务器查看服务是否起来:
ps ax | awk '/bin\/chasqui_start/ {print $1}' 没有输出。
但如果上去服务器上执行,服务能够正常起来。

deploy@prime-server-1:~/project_name$ cd /home/deploy/project_name/current && nohup ruby ./bin/chasqui_start >/dev/null 2>&1 &
[2] 2460
[1]   Done                    cd /home/deploy/project_name/current && nohup ruby ./bin/chasqui_start > /dev/null 2>&1
deploy@prime-server-1:~/project_name$ ps ax | awk '/bin\/chasqui_start/ {print $1}'
2500

另外,如果我手动在服务器上执行 bin/chasqui_start 之后,然后通过 capistrano 重新部署,chasqui_start 进程会挂掉,说明 execute "for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done" 还是会执行的,但是cd #{deploy_to}/current && nohup ruby ./bin/chasqui_start >/dev/null 2>&1 & 没有执行。

思索并搜索未果,故此一帖请教,多谢。

试试 Procfile

cd #{deploy_to}/current && nohup ruby ./bin/chasqui_start >/dev/null 2>&1 & 是这句没有指定 ruby 的版本?你可以把错误信息直接打印出来看看

感谢楼上两位;
@zouqilin 的指导下,应该是 nohup 还没有把服务成功启动,SSH 就退出了。
解决方法为,sleep x seconds

on roles(:chasqui) do
  execute "for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done"
  execute "bash -l -c 'cd #{deploy_to}/current && (nohup rvm use 2.2.4 do ruby bin/chasqui_start 2>&1 &) && sleep 2 && ps -ef | grep chasqui_start' "
end

部署结果

...
INFO [4926e2d3] Running for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done as deploy@server_a_ip
DEBUG [4926e2d3] Command: for i in $( ps ax | awk '/chasqui_start/ {print $1}' ); do kill ${i}; done
INFO [3bf1335b] Running bash -l -c 'cd /home/deploy/project_name/current && (nohup rvm use 2.2.4 do ruby bin/chasqui_start 2>&1 &) && sleep 2 && ps -ef | grep chasqui_start' as deploy@server_a_ip
DEBUG [3bf1335b] Command: bash -l -c 'cd /home/deploy/project_name/current && (nohup rvm use 2.2.4 do ruby bin/chasqui_start 2>&1 &) && sleep 2 && ps -ef | grep chasqui_start'
DEBUG [3bf1335b]    nohup: appending output to ‘nohup.out’
DEBUG [3bf1335b]    deploy   12008     1  0 23:57 ?        00:00:00 ruby bin/chasqui_start
DEBUG [3bf1335b]    deploy   15134 12310  3 23:59 pts/0    00:00:00 bash -l -c cd /home/deploy/project_name/current && (nohup rvm use 2.2.4 do ruby bin/chasqui_start 2>&1 &) && sleep 2 && ps -ef | grep chasqui_start
deploy   15235     1 10 23:59 pts/0    00:00:00 ruby bin/chasqui_start
deploy   15449 15134  0 23:59 pts/0    00:00:00 grep chasqui_start
INFO [3bf1335b] Finished in 2.248 seconds with exit status 0 (successful).
...
hww 关闭了讨论。 09月25日 13:41
需要 登录 后方可回复, 如果你还没有账号请 注册新账号