gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:49 26 4
gpt4 key购买 nike

我正在尝试使用 PHP 导出“top”命令 (unix) 的输出。使用“exex”函数调用和读取命令/输出非常简单,但问题是:“top”命令输出(顺序、名称等)是否始终相同?还是与我使用的发行版有关?

有什么方法可以在 UNIX 系统上“通用”地解析这些信息吗?

提前致谢

最佳答案

您可以使用 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

关于php - 使用 PHP 解析 "top"命令(Shell)的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com