output of top on a linux box

Jeff McNeil jeff at jmcneil.net
Mon Apr 9 13:32:55 EDT 2007


Can you pull the same information from /proc/stat as opposed to using
a pipe to top? The first line(s) should contain (at least):

cpu usermode, lowprio, system, idle, hz.

The 2.6 kernel adds iowait, irq, and soft irq.  It seems that this
might be a better solution than executing that additional command.

Take a look at proc(5).

Thanks,

Jeff





On 4/9/07, Pradnyesh Sawant <spradml at gmail.com> wrote:
> Hello,
> I need the cpu usage of a linux box, for which i capture the output of
> "top" using "popen". However, i am facing problems during string
> handling. The code snippet is:-
>
> top = os.popen("top -n 1")
> rd = top.read().split("\n")
> splt = rd[2].split()
> # cpu = splt[2][:-4]  # why ain't this working ???
> cpu = splt[2]
> print "cpu usage: %s, %s" % (cpu, type(cpu))
>
> the "print" statement prints, "cpu usage: 0.5%sy, <type 'str'>". i
> tried my best to get rid of the "%sy" after the "0.5", but couldn't
> succeed (even though the type of "cpu" is "string").
>
> Also, one weird thing that was happening was that if i still "split"
> the cpu string (removed the last 4 characters), the output of the next
> print statement was wrong (specifically, the first 2 characters got
> chipped off). This output is totally unexpected, and wrong...
>
> Can anyone kindly help me in understanding what exactly is going on here.
>
> Thanks a lot!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list