Is there any way to get ps output programmatically?

孤者浪人 提交于 2019-12-10 08:46:32

问题


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?


回答1:


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




回答2:


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.



回答3:


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.




回答4:


if you don't want to invoke PS then why don't you try with /proc file system.I think you can write you python program and read the files from /proc file system and extract the data you want.I did this using perl,by writing inlined C code in perl script.I think you can find similar way in python as well.I think its doable,but you need to go through /prof file system and need to figure out what you want and how you can get it. http://www.faqs.org/docs/kernel/x716.html above URL might give some initial push.



来源:https://stackoverflow.com/questions/3559157/is-there-any-way-to-get-ps-output-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!