Reputation: 4662
I can't install any modules on some box, thus - I can't use psutil
.
Need to get % of CPU usage by given PID.
One solution I see - use subprocess
, but it looks horribly:
# CPU usage
cpu_percentage = subprocess.call("top -p 25393 -b -n 1 | grep -w java | awk '{print $9}'", shell=True, stdout=devnull)
print('\nCPU percentage usage by Java: %s%%' % cpu_percentage)
Also, such way - I can't find out how to pass variable, instead of PID
directly (25393 in this exampel).
Upvotes: 0
Views: 1270
Reputation: 517
To pass the variable to a string you can format it just like you did on line 3:
# CPU usage
# +Apply here:
cpu_percentage = subprocess.call("top -p %d -b -n 1 | grep -w java | awk '{print $9}'" % PID, shell=True, stdout=devnull)
# +like you did here:
print('\nCPU percentage usage by Java: %s%%' % cpu_percentage)
PID
should be a int
instance for make this work
Upvotes: 1