解析" top"的输出用PHP命令(Shell)

时间:2014-04-01 14:50:52

标签: php linux shell parsing unix

我试图导出" top"的输出。使用PHP命令(unix)。使用" exex"调用和读取命令/输出非常简单功能,但问题是:是"顶部"命令输出(顺序,名称等)总是一样的?或者它与我使用?

的发行版有关

有没有办法解析这些信息"一般"在UNIX系统上?

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以使用 ps ,使用参数-A和-o,如下所示:

ps -Ao %cpu,%mem,user,comm

这将为您提供与调用命令时指定的输出完全相同的输出:

 0.0  0.1 root     udisks-daemon
 0.0  0.0 root     udisks-daemon
 0.0  0.1 root     gdm-simple-slav
11.0  0.4 root     Xorg
 0.0  0.1 root     NetworkManager
 0.0  0.1 root     polkitd
 0.0  0.1 root     gdm3

参数:

-A     Select all processes.

-o format
              User-defined format.  format is a single argument in the
              form of a blank-separated or comma-separated list, which
              offers a way to specify individual output columns.  The
              recognized keywords are described in the STANDARD FORMAT
              SPECIFIERS section below.  Headers may be renamed (ps -o
              pid,ruser=RealUser -o comm=Command) as desired.  If all
              column headers are empty (ps -o pid= -o comm=) then the
              header line will not be output.  Column width will
              increase as needed for wide headers; this may be used to
              widen up columns such as WCHAN (ps -o pid,wchan=WIDE-
              WCHAN-COLUMN -o comm).  Explicit width control (ps opid,
              wchan:42,cmd) is offered too.  The behavior of ps -o
              pid=X,comm=Y varies with personality; output may be one
              column named "X,comm=Y" or two columns named "X" and "Y".
              Use multiple -o options when in doubt.  Use the PS_FORMAT
              environment variable to specify a default as desired;
              DefSysV and DefBSD are macros that may be used to choose
              the default UNIX or BSD columns.

您可以使用的所有 STANDARD FORMAT SPECIFIERS ,您可以在ps的手册页中找到,但为方便起见,我也将它们复制到此处:

https://gist.github.com/ivankovacevic/9918272

答案 1 :(得分:0)

正如卢卡斯所提到的,ps可能正在印刷你期望的​​东西。

如果您的Unix风格是Linux,我认为pidstat(来自sysstat包)更适合您的需求(更不用说,在我看来以更清晰的方式记录)。< / p>