Python subprocess.CalledProcessError:返回非零退出状态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()

这是一个使用smartmontools和lm-sensors在Python中监控温度的小脚本。但是当我尝试运行它时,我遇到了一个错误

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

但是当我在终端中手动尝试这个命令时,它们工作得很好。

一些信息:

uname -a

Linux LME 4.0.0-040000-通用#201504121935 SMP Sun Apr 12 23:36:33 UTC 201504121935 x86_64 x86_64 x86_64 GNU/Linux

转载请注明出处:http://www.shenzhenzsmy.com/article/20230524/1885679.html