【问题标题】:Fabric: executing "systemctl status" on stopped processFabric:在停止的进程上执行“systemctl status”
【发布时间】:2019-12-21 01:23:01
【问题描述】:

我在 Red Hat 上使用 Fabric 2.5 时遇到了意外行为:

在相应进程运行时执行“systemctl status”,Fabric 按预期返回:

>>> from fabric import Connection
>>> hostname = 'foo.bar'
>>> c = Connection(host=hostname, user=foo, connect_kwargs={"password":"bar",})
>>> result = c.sudo('systemctl status postgresql-9.6.service)
postgresql-9.6.service - PostgreSQL 9.6 database server
 Loaded: loaded(/usr    /lib ...)  ...
Active: active (running) since ...  ...

但是,在停止的进程上运行相同的程序,Fabric 返回退出代码 3:

>>> result = c.sudo('systemctl stop postgresql-9.6.service)
>>> result = c.sudo('systemctl status postgresql-9.6.service)
postgresql-9.6.service - PostgreSQL 9.6 database server
 Loaded: loaded(/usr    /lib ...)  ...
Active: inactive (dead) since ...  ...
...
Traceback (most recent call list):
...
raise UnexpectedExit(result)
invoke.exceptions.UnexpectedExit: Encountered a bad command exit code!

Command: "sudo -S -p '[sudo] password: ' systemctl status postgresql-9.6.service"

Exit code: 3

Stdout: already printed

Stderr: n/a (PTYs have no stderr)

【问题讨论】:

    标签: python python-3.x fabric systemctl


    【解决方案1】:

    这是意料之中的。 status 将为停止的服务返回非零错误代码。为防止这引发异常,请将warn=True 添加到sudo 调用中。

    【讨论】: