Fabric:在已停止的进程上执行“ systemctl状态”

问题描述 投票:0回答:1

我在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个回答
0
投票

这是预期的。 status将针对停止的服务返回非零错误代码。为了防止这种情况引发异常,请将warn=True添加到sudo调用中。

© www.soinside.com 2019 - 2024. All rights reserved.