I'm trying to log the output of CPU and RAM consumption of specific processes in linux using python's os.system() command. To differentiate the output taken at each iteration, I've added print statements with the iteration number above and below this os.system() command.
My code is as below:
import os
i=0
while i<30:
print "----CPU----"
op=os.system("ps -raxxxo pid,%cpu,%mem,vsize,time,command | grep -E 'java|gui' ")
print i
i+=1
sleep(1.0/3.0)
Now, after running the script and taking a look at the log it doesnt meet my expectation. I see that for all iterations the output for os.system is printed first and then those print statements are at the end. This doesnt help me differentiate the output of each iteration.
Output after running the python script.py >& log.txt. (I'm running this script in linux env) - partial output
1332 0.0 3.6 572960 5:56.04 /data/bin/gui
20978 0.0 0.5 79480 1:47.16 java
13040 0.0 0.0 18976 0:00.00 grep -E java|gui
1332 0.0 3.6 572960 5:56.04 /data/bin/gui
20978 0.0 0.5 79480 1:47.16 java
13043 0.0 0.0 18976 0:00.00 grep -E java|gui
----CPU----
0
1
----CPU----
0
2
How can I have an output like below in python?
----CPU----
1332 0.0 3.6 572960 5:56.04 /data/bin/gui
20978 0.0 0.5 79480 1:47.16 java
13043 0.0 0.0 18976 0:00.00 grep -E java|gui
<iteration-number>
Looking forward to the community's answer and comments. Also, if this is a duplicate question please point me the similar one as I didn't get a similar Q&A for it.