python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令

2022-01-18 00:00:00 python subprocess popen

问题描述

用于识别 Xcode 是否在 Mac 上运行的命令框架:cmd = "ps -ax | grep -v grep | grep Xcode"

Command framed to identify if Xcode is running on Mac: cmd = "ps -ax | grep -v grep | grep Xcode"

如果 Xcode 没有运行,那么上面的命令适用于 subprocess 模块的 Popen 方法,但会引发 CalledProcessErrorcheck_output 方法.我试图通过以下代码检查stderr,但未能获得适当的信息来理解原因.

If Xcode is not running, then above command works well with Popen method of subprocess module, but raises a CalledProcessError with check_output method. I tried to inspect the stderr through the following code, but failed to get appropriate information to understand the reason.

from subprocess import check_output, STDOUT, CalledProcessError

psCmd = "ps -ax | grep -v grep | grep Xcode"
o = None
try:
    o = check_output(psCmd, stderr=STDOUT, shell=True)
except CalledProcessError as ex:
    print 'Error:', ex, o

异常信息如下:

错误:命令'ps -ax |grep -v grep |grep Xcode' 返回非零退出状态 1 无

问题:为什么上面的命令对 Popen 有效,但由于 check_output 失败?

Question: Why the above command works with Popen, but fails with check_output ?

注意:如果 Xcode 正在运行,命令对这两种方法都适用.

Note: Command works well with both approach, if Xcode is running.


解决方案

check_output() 按预期工作.下面是它在 Popen() 方面的简化实现:

check_output() works as expected. Here's its simplified implementation in terms of Popen():

def check_output(cmd):
    process = Popen(cmd, stdout=PIPE)
    output = process.communicate()[0]
    if process.returncode != 0:
        raise CalledProcessError(process.returncode, cmd, output=output)
    return output

grep 如果没有找到任何东西,则返回 1,也就是说,如果 Xcode 没有运行,您应该期待异常.

grep returns 1 if it hasn't found anything i.e., you should expect the exception if Xcode is not running.

注意:如实现所示,即使发生异常也能得到输出:

Note: as the implementation shows, you can get the output even if the exception occurs:

#!/usr/bin/env python
from subprocess import check_output, STDOUT, CalledProcessError

cmd = "ps -ax | grep -v grep | grep Xcode"
try:
    o = check_output(cmd, stderr=STDOUT, shell=True)
    returncode = 0
except CalledProcessError as ex:
    o = ex.output
    returncode = ex.returncode
    if returncode != 1: # some other error happened
        raise

您可以改用 pgrep -a Xcode 命令(注意:以 p 开头)或使用 psutil 模块作为可移植代码:

You could probably use pgrep -a Xcode command instead (note: starts with p) or use psutil module for a portable code:

#!/usr/bin/env python
import psutil # $ pip install psutil

print([p.as_dict() for p in psutil.process_iter() if 'Xcode' in p.name()])

相关文章