python - Fabric:在停止的进程上执行 "systemctl status"

标签 python python-3.x fabric systemctl

我在 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/

相关文章:

python - 使用 pyenv 切换 python 版本

python - 'str' 对象没有属性 'decode'

django - 我的 Django/uWSGI vassal 的堆栈跟踪记录在哪里?

python - Fabric 中的 YAML 文件

python - Virtualenv 下的结构 : How to obtain the system's Python library path (not the library path of the virtualenv Python)

python - 如何在 Python + Tkinter 中获取菜单复选按钮来调用函数?

python - Telegram bot 记录错误代码 409,即使有单个实例正在运行

python - 一种优雅、更短的方式来解决字母等级分配问题

python-3.x - 如何将循环中的字符串附加到单个输出行?

python Fabric : filter out server output when capturing output of run()