Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print subprocess.call result [duplicate]

I am trying to return the temperature of my drive in a single string with "Temp: " as a prefix. To get there I run a simple script for concatenating a string with the output of a command.

import subprocess

command = "sudo smartctl -A /dev/sda | egrep Temperature_Celsius | awk '{print $10}'"

print "Temp " + str(subprocess.call(command, shell=True))


Result:

29
Temp 0


When I delete the 'print' line, the '29' also does not show. So when I use a print statement, the script returns the '29' for some reason (?!) and then returns 0 out of the blue while the 29 is actually the right value.

I wish I would get this:

Temp: 29

I have tried to use os.system and that gives the same results. I have also tried to add the command directly into the "command = str(subprocess.call(" but it gave the same results. I've read the os.system info pages and the subprocess.call info pages, I did not find a solution. Then I Googled for the AWK command, maybe it creates some crazy output but I can't find anything useful.

Python 2.7 on Linux 3.8.0-34-generic #49-Ubuntu SMP Tue Nov 12 18:00:10 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

like image 281
Thijs Avatar asked Mar 21 '23 09:03

Thijs


1 Answers

subprocess.call() returns the process exit code (where 0 means success). If you wanted the process output, use subprocess.check_output() instead:

print "Temp " + subprocess.check_output(command, shell=True)
like image 67
Martijn Pieters Avatar answered Apr 19 '23 06:04

Martijn Pieters