Is there any way to get ps output programmatically? Is there any way to get ps output programmatically? unix unix

Is there any way to get ps output programmatically?


You could check out this question about parsing ps output using Python.

One of the answers suggests using the PSI python module. It's an extension though, so I don't really know how suitable that is for you.

It also shows in the question how you can call a ps subprocess using python :)


My preference is to do something like this.

collection.sh

for (( ;; ))do    date; ps -p $1 -o '%cpu'done

Then run collection.sh >someFile while you "slam the server with requests".

Then kill this collection.sh operation after the server has been slammed.At the end, you'll have file with your log of date stamps and CPU values.

analysis.py

import datetimewith( "someFile", "r" ) as source:    for line in source:        if line.strip() == "%CPU": continue        try:            date= datetime.datetime.strptime( line, "%a %b %d %H:%M:%S %Z %Y" )        except ValueError:            cpu= float(line)            print date, cpu # or whatever else you want to do with this data.


You could query the CPU usage with PySNMP. This has the added benefit of being able to take measurements from a remote computer. For that matter, you could install a VM of Zenoss or its kin, and let it do the monitoring for you.