我在 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)
最佳答案
这是预期的。 status 将为已停止的服务返回非零错误代码。为了防止引发异常,请将 warn=True
添加到 sudo
调用中。
关于python - Fabric:在停止的进程上执行 "systemctl status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57495485/