linux - shell 脚本中 top 命令的 CPU 信息

标签 linux bash shell

这是 top 命令的输出:

Mem: 245108K used, 784400K free, 6636K shrd, 15128K buff, 41824K cached
CPU:  12% usr   0% sys   0% nic  87% idle   0% io   0% irq   0% sirq
Load average: 2.62 2.58 2.57 2/90 29509

我需要一个单行脚本命令来执行并获取“CPU”行。

我可以从“cat/proc/meminfo”获取Mem行,并从“cat/proc/loadavg”获取平均负载。

有什么方法可以让 CPU 达到相同的效果吗?

最佳答案

@md.jamal -- 尝试此操作,获取当前使用资源最多的 10 个进程 ID(包含 %CPU 和 %MEM)。

    ps -eo pid,ppid,%cpu,%mem | sort -rnk3 | head

Below is details for above command:

where ps --> reports snapshot of current process
-e --> select all processes
-o --> output format
sort -r --> reverse sort, ie descending order
sort -n --> numeric sort
sort -k3 --> to select column no:3
head --> to display top 10 records

关于linux - shell 脚本中 top 命令的 CPU 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48533978/

相关文章:

regex - 有没有在 bash 中用代字号替换主目录的好方法?

linux - 在 "find -exec"中使用 eval 时出现意外行为

php - 如何使用来自 php 的一些参数运行 perl 脚本

bash - 为什么在可执行文件或脚本名称之前需要 ./(点斜杠)才能在 bash 中运行它?

java eclipse 插件 : menuitems appear in windows but not on linux

linux - 具有多个目标的 Makefile

linux - 确定被 oom-killer 杀死的进程的虚拟机大小

linux - 在 X 启动时执行 JAR

Linux Shell - 如何将变量分配给二进制表达式?

linux - 执行 ls -lh 命令