Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get ps output programmatically?

I've got a webserver that I'm presently benchmarking for CPU usage. What I'm doing is essentially running one process to slam the server with requests, then running the following bash script to determine the CPU usage:

#! /bin/bash

for (( ;; ))
do

    echo "`python -c 'import time; print time.time()'`, `ps -p $1 -o '%cpu' | grep -vi '%CPU'`"
    sleep 5
done

It would be nice to be able to do this in Python so I can run it in one script instead of having to run two. I can't seem to find any platform independent (or at least platform independent to linux and OS X) way to get the ps output in Python without actually launching another process to run the command. I can do that, but it would be really nice if there were an API for doing this.

Is there a way to do this, or am I going to have to launch the external script?

like image 248
Jason Baker Avatar asked Feb 26 '23 09:02

Jason Baker


2 Answers

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 :)

like image 114
extraneon Avatar answered Mar 07 '23 15:03

extraneon


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 datetime
with( "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.
like image 29
S.Lott Avatar answered Mar 07 '23 15:03

S.Lott