Quick Silver
Quick Silver

Reputation: 433

writing to a file , subprocess. call output returns nothing

I have a small code which tracks down cpu use periodically. Somehow when I try to create a file ( "wb" or "w" mode) the file is created but its empty. Any idea why is it so?

W/O the file handler:

import subprocess
import os

MESSAGE = "mpstat -P ALL | awk '{print $4}'"
SLEEP = "sleep 1"

cmds = [MESSAGE, SLEEP]


def runCommands(commands = cmds):
    count =0
    while True:
            for cmd in cmds:
                    count+=1
                    subprocess.call(cmd, shell = True)


runCommands()

With file handler:

import subprocess
import os

MESSAGE = "mpstat -P ALL | awk '{print $4}'"
SLEEP = "sleep 1"

cmds = [MESSAGE, SLEEP]


def runCommands(commands = cmds):
    count =0
    while True:
         for cmd in cmds:
            count+=1
            with open('cpu_usage.txt', 'w')as f:
                subprocess.call(cmd, stdout = f, shell = True)


runCommands()

mpstat gives a standard output ( not a standard error ). The objective is to gather cpu and memory usage every second using python and embed in an application to gather the data and graphically output the same. I know that psutil is a good framework in this regard but if you didnt play with it much. It could also solve my issue given the fact that in the end i have a graphical output which contains mem and cpu use per sec.

Eventually I am looking for an output which is of the form:

      %CPU   %MEM 

      ..      ..
      ..      ..
      ..      ..

and in the end a time vs CPU and time vs memory will suffice the need. I am just one step into this problem by grabbing the cpu value. ps aux doesnt seem to be a good command to do what I need , although it gives the output similar to the one I want. Any ideas/thoughts/suggestions.

Upvotes: 1

Views: 781

Answers (1)

Martin B.
Martin B.

Reputation: 1928

When you're opening a file with the 'w' argument, it is created anew every time, meaning that when your while loop finished (it wouldn't in your example, but let's assume it does) - the last thing executed with the file is the sleep 1 command which doesn't print anything. Open the file with the 'a' (append) flag and you'll have all the mpstat outputs in it.

See http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files for full reference.

But in general, try to do more processing in Python and rely less on OS commands, like below (though I'd still not do the AWK stuff at all, but anyway).

import subprocess
import os
import time

CPU = "mpstat -P ALL | awk 'NR==4 { print $3 }'"
MEM = "free -m | awk 'NR==3 { print $4 }'"

def runCommands():
    count = 0
    f = open('cpu_usage.txt', 'a')
    while True:
         t = str(int(time.time()))
         cpu = subprocess.check_output(CPU, shell = True).strip()
         mem = subprocess.check_output(MEM, shell = True).strip()

         f.write(' '.join([t, cpu, mem]))
         f.write('\n')
         f.flush()

         time.sleep(1)

runCommands()

Upvotes: 2

Related Questions