我发现 shell 命令的输出top
是一种简单而熟悉的方法,可以粗略地了解机器的运行状况。我想top
从机器上的小型网络服务器提供 的输出(或与之非常相似的东西),以进行粗略的监控。
有没有办法让 top 准确地编写其文本输出一次,没有格式化字符?我试过这个:
(sleep 1; echo 'q') | top > output.txt
这似乎是关闭到我想要的,除了(1)不能保证我不会得到多于或少于一屏的信息,(2)我必须删除所有终端格式字符。
或者是否有其他top
类似的命令可以列出机器范围和进程级别的内存/CPU 使用情况/正常运行时间信息?
(理想情况下,我希望有一种可移植到 Linux 和 Mac OS X 的策略,因为我们的开发人员使用 Mac,而我们的产品环境是 Linux。)
答案1
在Linux中,你可以尝试这个:
top -bn1 > output.txt
从man top
:
-b : Batch-mode operation
Starts top in 'Batch' mode, which could be useful for sending
output from top to other programs or to a file. In this
mode, top will not accept input and runs until the iterations
limit you've set with the '-n' command-line option or until
killed.
....
-n : Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top
should produce before ending.
对于 OS X,请尝试:
top -l 1
-l <samples>
Use logging mode and display <samples> samples, even if
standard output is a terminal. 0 is treated as infinity.
Rather than redisplaying, output is periodically printed in
raw form. Note that the first sample displayed will have an
invalid %CPU displayed for each process, as it is calculated
using the delta between samples.
答案2
答案3
macOS 黑客
抱歉,这太糟糕了,但它让我在 macOS 上得到了我想要的东西:
top -l 2 -n 10 | tail -22
-l 2
- 根据评论,如果给定只有一个样本大小,macos 将在 CPU 字段中输出 0。因此,采样两次。-n 10
仅采用顶级进程(按 CPU 负载是默认顺序)tail -22
- 真正的 hackish 部分 - 跳过有关第一个示例的行,-22
在本例中完全是通过反复试验,并且与-n 10
.
以此为荣?绝不是,但它大致满足了我想要的。
Processes: 547 total, 3 running, 544 sleeping, 2558 threads
2022/11/24 17:18:44
Load Avg: 1.74, 2.05, 2.01
CPU usage: 6.25% user, 1.71% sys, 92.2% idle
SharedLibs: 257M resident, 35M data, 14M linkedit.
MemRegions: 229362 total, 3865M resident, 114M private, 1192M shared.
PhysMem: 16G used (3357M wired), 143M unused.
VM: 11T vsize, 2317M framework vsize, 11336543(0) swapins, 13536939(0) swapouts.
Networks: packets: 33586023/7445M in, 34143918/6707M out.
Disks: 9964939/225G read, 3781186/116G written.
PID COMMAND %CPU TIME #TH #WQ #PORTS MEM PURG CMPRS PGRP PPID STATE BOOSTS %CPU_ME %CPU_OTHRS UID FAULTS COW MSGSENT MSGRECV SYSBSD SYSMACH CSW PAGEINS IDLEW POWER INSTRS CYCLES USER #MREGS RPRVT VPRVT VSIZE KPRVT KSHRD
17013 postgres 28.5 01:00.77 1 0 8 62M+ 0B 0B 17013 16225 sleeping *0[1] 0.00000 0.00000 501 545151+ 124 152 5 353119+ 118842+ 11731+ 0 0 28.5 3760288663 1617021064 postgres N/A N/A N/A N/A N/A N/A
17019 postgres 25.9 01:04.16 1/1 0 8 64M+ 0B 0B 17019 16225 running *0[1] 0.00000 0.00000 501 552890+ 123 160+ 5 359735+ 120494+ 11930+ 0 0 25.9 3263636650 1446362124 postgres N/A N/A N/A N/A N/A N/A
17329 top 8.8 00:00.52 1/1 0 28+ 3728K+ 0B 0B 17329 13212 running *0[1] 0.03698 0.00000 0 2471+ 106+ 517539+ 255643+ 4594+ 264177+ 48+ 0 0 8.8 388290277 451214497 root N/A N/A N/A N/A N/A N/A
16245 Python 8.3 08:43.72 1 0 21 1027M+ 0B 1104K 16245 37017 sleeping *0[1] 0.00000 0.00000 505 1008120+ 3429 3229 41 8420748+ 6791 2448946+ 87 35 8.3 442524899 451503337 me__ N/A N/A N/A N/A N/A N/A
17015 postgres 5.1 00:12.19 1 0 8 29M+ 0B 0B 17015 16225 sleeping *0[1] 0.00000 0.00000 501 8276+ 114 14 5 360531+ 30 85127+ 0 5 5.1 516698298 269517335 postgres N/A N/A N/A N/A N/A N/A
17020 postgres 4.8 00:12.64 1 0 8 32M+ 0B 0B 17020 16225 sleeping *0[1] 0.00000 0.00000 501 9074+ 113 14 5 400661+ 34 94522+ 0 6 4.8 486455789 262738456 postgres N/A N/A N/A N/A N/A N/A
0 kernel_task 3.7 01:46:28 287/16 0 0 1436M 0B 0B 0 0 running 0[0] 0.00000 0.00000 0 6106435 9589 331617464+ 300334081+ 0 0 615407045+ 183 32498664+ 0.0 96723302 461786276 root N/A N/A N/A N/A N/A N/A
15976 Python 3.0 01:39.90 2 0 24 90M 0B 4860K 94785 94785 sleeping *0[1] 0.00000 0.00000 505 26811 643 460 21 6928600+ 1008 28526+ 448 1886+ 3.1 235167151 177692860 me__ N/A N/A N/A N/A N/A N/A
16226 postgres 1.6 00:39.95 1 0 8 728K 0B 188K 16226 16225 sleeping *0[1] 0.00000 0.00000 501 340 73 23 5 10634810+ 20 2383516+ 0 4 1.6 37954507 77088931 postgres N/A N/A N/A N/A N/A N/A
150 WindowServer 1.4 04:23:51 16 7 2944 1307M- 19M 107M 150 1 sleeping *0[1] 0.02412 0.01154 88 15718938+ 72229 255258396+ 99186014+ 280197394+ 409489438+ 118640818+ 3284 3028879 1.4 29648136 64463377 _windowserver N/A N/A N/A N/A N/A N/A