解析"top"输出.使用PHP的命令(Shell) [英] Parsing output of "top" command (Shell) with PHP
问题描述
我正在尝试使用PHP导出"top"命令(unix)的输出.使用"exex"功能调用和读取命令/输出非常简单,但问题是:"top"命令输出(顺序,名称等)是否始终相同?还是和我使用的发行版有关?
I'm trying to export the output of the "top" command (unix) with PHP. Invoking and reading the command/output is pretty simple using the "exex" function but the question is: is the "top" command output (order, names, etc.) always the same? Or is it related to the distribution I'm using?
在UNIX系统上,有什么方法可以一般"地解析这些信息吗?
Is there any way to parse these information "generally" on UNIX systems?
预先感谢
推荐答案
您可以改为使用 ps ,并带有-A和-o参数,如下所示:
You could use ps instead, with arguments -A and -o like so:
ps -Ao %cpu,%mem,user,comm
这将为您提供与调用命令时指定的输出完全相同的输出:
which would give you the output exactly like you specified when you called the command:
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.
您可以使用的所有标准格式规范,您都可以在ps的手册页中找到,但是为了方便起见,我也在此处复制了它们:
All STANDARD FORMAT SPECIFIERS that you could use, you can find in the man page of ps, but I've copied them also here for convenience:
https://gist.github.com/ivankovacevic/9918272
这篇关于解析"top"输出.使用PHP的命令(Shell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!