You were almost there: just needed to "escape" the quotes ( i.e. use print \"
instead of print "
, etc) to get the first line right:
import os, multiprocessing
p = os.system("ps aux|awk 'NR > 0 { s +=$3 }; END {print \"cpu %\",s}'")
And you should recognize that the os.system
command returns the "return code" - the answer to the question "did this process end normally?". Thus the value of p
is 0
"no error", not the cpu load which was in the variable s
. Not sure why you need it twice since you already printed it out. Change the last line to
print "Cores:", multiprocessing.cpu_count(), '\n'
And the output becomes something like:
cpu% 65.7
Cores: 2
If you actually want to get the output of your system command straight into a variable, you could look at the answer given to this earlier question - it shows you how to use subprocess.Popen()
and proc.communicate()
to do exactly that. But just getting the command line right (with the escaped quote) is the most critical fix.
update two complete and working examples of the Popen() and system() methods follow:
import os, multiprocessing, subprocess
# two ways to get CPU load and number of CPUs
# command line string to obtain CPU load:
commandString = "ps aux|awk 'NR > 0 { s+=$3 }; END {print \"CPU%\", s}'"
print "Command string used to obtain CPU load:"
print commandString, "\n"
# obtain number of CPUs from the multiprocessing function:
numCPUs = multiprocessing.cpu_count()
# method 1: issue an os.system command, have the result echoed directly to output:
print "Method 1:"
p = os.system(commandString)
print "Cores:", numCPUs, "\n"
# method 2: return the value to a python variable:
cpu = subprocess.Popen(commandString, stdout=subprocess.PIPE, shell=True)
(out, err) = cpu.communicate()
print "Method 2:"
print "Load: ", out,
print "Cores:", multiprocessing.cpu_count(), '\n'
Output of the above:
Command string used to obtain CPU load:
ps aux|awk 'NR > 0 { s+=$3 }; END {print "CPU%", s}'
Method 1:
CPU% 18.5
Cores: 2
Method 2:
Load: CPU% 24.1
Cores: 2