Python subprocess.CalledProcessError: returns nonzero 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 a small script for temperature control in Python using smartmontools and lm sensors. But when I try to run it, I have an error

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

But when I try to execute this command manually in the terminal, they work fine.

Some information:

uname -a 

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

+4
source share
2 answers

A CalledProcessError , - . echo $? , 2. , .

Python, CalledProcessError , output. ( python docs .)

:

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

2 smartctl , . , , Python, , .

RETURN VALUES smartctl:

1: , IDENTIFY DEVICE

, . . subprocess.check_output( [ 'smartctl', '-A', '/dev/sda2' ] ), 2, subprocess.check_output( [ 'sudo', 'smartctl', '-A', '/dev/sda2' ] ), , .

+1

Source: https://habr.com/ru/post/1584264/


All Articles