简体   繁体   English

Python检查shell命令的退出状态

[英]Python check exit status of a shell command

# function to run shell commands #function运行shell命令

def OSinfo(runthis):
        #Run the command in the OS
        osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
        #Grab the stdout
        theInfo = osstdout.stdout.read() #readline()
        #Remove the carriage return at the end of a 1 line result
        theInfo = str(theInfo).strip()
        #Return the result
        return theInfo

# flash raid firmware #flash raid固件

OSinfo('MegaCli -adpfwflash -f ' + imagefile + ' -noverchk -a0')

# return status of the firmware flash #返回固件闪存状态

?

One resource recommended using 'subprocess.check_output()', however, I'm not sure how to incorporate this into function OSinfo(). 一个资源建议使用'subprocess.check_output()',但是,我不知道如何将其合并到函数OSinfo()中。

If you just want to return 1 if there is a non-zero exit status use check_call , any non zero exit status will raise an error which we catch and return 1 else osstdout will be 0 : 如果你只想要return 1如果有一个非零退出状态使用check_call ,任何非零退出状态将引发一个错误,我们捕获并return 1否则osstdout将为0

import subprocess
def OSinfo(runthis):
        try:
            osstdout = subprocess.check_call(runthis.split())
        except subprocess.CalledProcessError:
            return 1
        return osstdout

You also don't need shell=True if you pass a list of args. 如果传递args列表,也不需要shell = True。

Instead of using osstdout.stdout.read() to get the stdout of the subprocess you can instead use osstout.communicate() This will block until the subprocess terminates. 您可以使用osstout.communicate()而不是使用osstdout.stdout.read()来获取子osstdout.stdout.read()stdout ,这将阻塞,直到子osstout.communicate()终止。 Once this is done the attribute osstout.returncode will be set containing the return code of the subprocess. 完成此操作后,将设置属性osstout.returncode其中包含子osstout.returncode的返回码。

Your function could then be written as 然后你的函数可以写成

def OSinfo(runthis):
    osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)

    theInfo = osstdout.communicate()[0].strip()

    return (theInfo, osstout.returncode)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查 ipython 中最后一个命令的退出状态 - Check the exit status of last command in ipython Python函数subprocess.check_output返回CalledProcessError:命令返回非零退出状态 - Python function subprocess.check_output returns CalledProcessError: command returns non-zero exit status python check_output失败,退出状态为1,但Popen适用于同一命令 - python check_output fails with exit status 1 but Popen works for same command 获取Python脚本中最后一个命令的退出状态 - Get exit status of last command in Python script RROR:命令出错,退出状态为 1 Python - RROR: Command errored out with exit status 1 Python Python:错误:命令出错,退出状态为 1:python setup.py egg_info 检查日志以获取完整命令 output - Python: ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output 命令出错,退出状态为 1:python setup.py egg_info 检查日志以获取完整命令 output - Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output 协作错误:错误:命令出错,退出状态 1:python setup.py egg_info 检查日志以获取完整的命令输出 - Collab error:ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output 错误:命令出错,退出状态为 1:python setup.py egg_info 检查日志以获取完整命令 output。 [烧瓶] - ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. [FLASK] COLAB 错误:命令出错,退出状态为 1:python setup.py egg_info 检查日志以获取完整命令 output - COLAB ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM