[英]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.