有什么办法以编程方式获取ps输出吗? [英] Is there any way to get ps output programmatically?

查看:52
本文介绍了有什么办法以编程方式获取ps输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务器,目前正在对CPU使用情况进行基准测试.我要做的实际上是运行一个进程,用请求对服务器进行猛击,然后运行以下bash脚本来确定CPU使用率:

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

能够在Python中执行此操作很好,因此我可以在一个脚本中运行它,而不必运行两个脚本.我似乎找不到任何独立于平台(或至少独立于Linux和OS X的平台)的方式来获取Python中ps的输出,而无需实际启动另一个进程来运行命令.我可以做到,但是如果有一个API可以做到,真的很好.

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?

推荐答案

您可以查看

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

答案之一是建议使用 PSI 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.

它还在问题中显示了如何使用python调用ps子进程:)

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

这篇关于有什么办法以编程方式获取ps输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆