简体   繁体   English

Python subprocess.CalledProcessError:返回非零退出状态 2

[英]Python subprocess.CalledProcessError: returned non-zero exit status 2

#!/usr/bin/env python
# encoding: utf-8

import re
import subprocess
import time
import json


def get_temperatures(disks):
    sensors = subprocess.check_output(["sensors"])
    temperatures = {match[0]: float(match[1]) for match in re.findall("^(.*?)\:\s+\+?(.*?)°C", 
                                            sensors, re.MULTILINE)}
    for disk in disks:
        output = subprocess.check_output(["smartctl", "-A", disk])
        temperatures[disk] = int(re.search("Temperature.*\s(\d+)\s*(?:\([\d\s]*\)|)$", 
                                            output, re.MULTILINE).group(1))
    return temperatures


def main():
    while True:
        print json.dumps(get_temperatures(("/dev/sda2", "/dev/sdb1")))
        time.sleep(20)


if __name__ == '__main__':
    main()

This is small script to monitor temperatures in Python, using smartmontools and lm-sensors.这是使用 smartmontools 和 lm-sensors 在 Python 中监控温度的小脚本。 But when i try to run it i have a error但是当我尝试运行它时出现错误

subprocess.CalledProcessError: Command '['smartctl', '-A', '/dev/sda2']' returned non-zero exit status 2

But when i try this command manually in terminal they work great.但是当我在终端中手动尝试这个命令时,它们工作得很好。

Some info:一些信息:

uname -a 
 Linux LME 4.0.0-040000-generic #201504121935 SMP Sun Apr 12 23:36:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

A CalledProcessError will be raised if any non-zero exit code is returned by your called process.如果您的被调用进程返回任何非零退出代码,则会引发CalledProcessError On the command line, you should echo $?在命令行上,您应该echo $? to get the last return code and see if it really does return 2. I suspect it will.获取最后一个返回代码,看看它是否真的返回 2。我怀疑它会。

If that is okay in your python code, you can except the CalledProcessError and get any information from within its attributes especially the output attribute.如果这在您的 python 代码中没问题,您可以CalledProcessError并从其属性中获取任何信息,尤其是output属性。 (Look up this error in the python docs for more information.) (在python 文档中查找此错误以获取更多信息。)

Example:例子:

import subprocess
output = None
try:
    output = subprocess.check_output(["smartctl", "-A", "/dev/sda2"])
except subprocess.CalledProcessError as e:
    output = e.output

Return code of 2 from smartctl means it failed to open the device. smartctl返回码 2 表示无法打开设备。 Make sure that the user who is running the Python code has permission to open all of the disks you want it to check.确保运行 Python 代码的用户有权打开您希望它检查的所有磁盘。

From the RETURN VALUES section of smartctl's man page:从 smartctl 手册页的返回值部分:

Bit 1: Device open failed, or device did not return an IDENTIFY DEVICE structure位 1:设备打开失败,或设备未返回 IDENTIFY DEVICE 结构

So I suspect this is really a permissions problem.所以我怀疑这真的是一个权限问题。 I verified this on my system.我在我的系统上验证了这一点。 If I run subprocess.check_output( [ 'smartctl', '-A', '/dev/sda2' ] ) I get the error with it returning 2, but if I run subprocess.check_output( [ 'sudo', 'smartctl', '-A', '/dev/sda2' ] ) it works and I see the output of the command.如果我运行subprocess.check_output( [ 'smartctl', '-A', '/dev/sda2' ] )我得到错误返回 2,但如果我运行subprocess.check_output( [ 'sudo', 'smartctl', '-A', '/dev/sda2' ] )它工作,我看到命令的输出。

暂无
暂无

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

相关问题 subprocess.CalledProcessError: ...返回非零退出状态 255 - subprocess.CalledProcessError: ... returned non-zero exit status 255 subprocess.CalledProcessError:返回非零退出状态0 - subprocess.CalledProcessError: returned non-zero exit status 0 subprocess.CalledProcessError返回非零退出状态1 - subprocess.CalledProcessError returned non-zero exit status 1 Python subprocess.CalledProcessError:命令'adb设备'返回非零退出状态127 - Python subprocess.CalledProcessError: Command 'adb devices' returned non-zero exit status 127 Python 3.9.6 - subprocess.run - subprocess.CalledProcessError:命令 pip 安装 pkg 返回非零退出状态 1 - Python 3.9.6 - subprocess.run - subprocess.CalledProcessError: Command pip install pkg returned non-zero exit status 1 "subprocess.CalledProcessError:命令“df”返回非零退出状态 1" - subprocess.CalledProcessError: Command 'df' returned non-zero exit status 1 运行Python Cookiecutter以从GitHub中获取模板的结果为subprocess.CalledProcessError,返回非零退出状态128 - Running Python Cookiecutter to get template from GitHub results in subprocess.CalledProcessError, returned non-zero exit status 128 Python:wifi subprocess.CalledProcessError:命令'['/ sbin / ifdown','wlp4s0']'返回非零退出状态1 - Python: wifi subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlp4s0']' returned non-zero exit status 1 subprocess.CalledProcessError:返回非零退出状态 1,而 os.system 不会引发任何错误 - subprocess.CalledProcessError: returned non-zero exit status 1, while os.system does not raise any error subprocess.CalledProcessError…返回非零退出状态2 - subprocess.CalledProcessError… returns non zero exit status 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM