我在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)
答案 0 :(得分:0)
这是预期的。 status将针对停止的服务返回非零错误代码。为了防止这种情况引发异常,请将warn=True
添加到sudo
调用中。