24

我正在使用fabric 远程启动微型aws 服务器,安装git 和git 存储库,调整apache 配置,然后重新启动服务器。

如果在任何时候,从我发出的fabfile

sudo('service apache2 restart')run('sudo service apache2 restart')停止然后启动,命令显然运行,我得到指示 apache 已启动的响应,例如

[ec2-184-73-1-113.compute-1.amazonaws.com] sudo: service apache2 start
[ec2-184-73-1-113.compute-1.amazonaws.com] out:  * Starting web server apache2
[ec2-184-73-1-113.compute-1.amazonaws.com] out:    ...done.
[ec2-184-73-1-113.compute-1.amazonaws.com] out: 

但是,如果我尝试连接,连接会被拒绝,如果我 ssh 进入服务器并运行 sudo service apache2 status它会显示“ Apache is NOT running

在 sshed 中,如果 run sudo service apache start,服务器已启动并且我可以连接。有没有其他人经历过这个?或者是否有人对我可以查看的位置、日志文件等有任何提示,以了解发生了什么。或apache2/error.log中没有任何内容。syslogauth.log

这没什么大不了的,我可以解决它。我只是不喜欢这种无声的失败。

4

6 回答 6

41

您正在运行哪个版本的面料?

您是否尝试过改变pty论点(也尝试改变shell,但它不应该影响事情)?

http://docs.fabfile.org/en/1.0.1/api/core/operations.html#fabric.operations.run

您可以pty像这样设置参数:

sudo('service apache2 restart', pty=False)
于 2011-06-16T22:55:49.563 回答
14

试试这个:

sudo('service apache2 restart',pty=False)

遇到同样的问题后,这对我有用。我不确定为什么会这样。

于 2011-06-17T12:41:29.350 回答
4

当代表授予足够权限的用户(例如 root)连接到您的遥控器时,您可以管理系统服务,如下所示:

from fabtools import service

service.restart('apache2')

https://fabtools.readthedocs.org/en/0.13.0/api/service.html

PS它需要安装fabtools

pip install fabtools

于 2013-04-03T18:53:50.557 回答
4

这是此问题的一个实例, FAQ 中有一个条目包含 pty 答案。不幸的是,在 CentOS 6 上不支持 pty-less sudo 命令,我不喜欢这个nohup解决方案,因为它会杀死输出。

The final entry in the issue mentions using sudo('set -m; service servicename start'). This turns on Job Control and therefore background processes are put in their own process group. As a result they are not terminated when the command ends.

于 2014-01-09T20:36:57.533 回答
0

解决问题的更多方法。

  • 您可以使用 --no-pty 选项运行 fab 目标

    fab --no-pty <task>
    
  • 在 fabfile 中,在目标代码执行之前将全局环境变量 always_use_pty 设置为 False

    env.always_use_pty = False
    
于 2011-07-20T22:12:58.683 回答
0

使用 pty=False 仍然没有为我解决它。最终为我工作的解决方案是做一个双 nohup,如下所示:

运行.sh

#! /usr/bin/env bash
nohup java -jar myapp.jar 2>&1 &

工厂文件.py

...
sudo("nohup ./run.sh &> nohup.out", user=env.user, warn_only=True)
...
于 2014-01-08T18:57:52.583 回答