如何以可读格式将 Linux top命令输出捕获到文件中
【摘要】 准备尝试将top 命令的输出捕获到文件中。当我执行 top > output.txt 时,output.txt 文件包含很多垃圾字符。将 top 命令的输出捕获到可读文本文件中的最佳方法是什么?
使用 top 命令批处理模式操作选项 ( -b ) 将 top 命令输出捕获到文件中。
准备尝试将top 命令的输出捕获到文件中。当我执行 top > output.txt 时,output.txt 文件包含很多垃圾字符。将 top 命令的输出捕获到可读文本文件中的最佳方法是什么?
使用 top 命令批处理模式操作选项 ( -b ) 将 top 命令输出捕获到文件中。
如果您尝试将top命令输出重定向到如下所示的文本文件,您会注意到输出文件包含大量垃圾字符。
当您尝试使用 less 命令查看输出文件时,您会注意到创建的输出文件包含大量垃圾字符。
$ top -n 1 > top-output.txt
$ less top-output.txt
"top-output.txt" may be a binary file. See it anyway?
注意:选项 -n 1 表示只应执行 top 命令的一次迭代。
要避免此问题并获得可读的 top 命令输出,请在 top 命令中使用选项 -b。以批处理模式执行 top 命令,如下图所示。
$ top -n 1 -b > top-output.txt
$ less top-output.txt
top - 16:56:36 up 246 days, 11:14, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 168 total, 1 running, 167 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 8306856k total, 7940744k used, 366112k free, 285136k buffers
Swap: 8385920k total, 104k used, 8385816k free, 7391824k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2064 592 512 S 0.0 0.0 0:02.24 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.47 migration/0
3 root 35 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
5 root RT -5 0 0 0 S 0.0 0.0 0:00.61 migration/1
关于top命令手册页中的选项 -b 和 -n:
-b : Batch mode operation
Starts top in "Batch mode", which could be useful for sending out-
put 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.
您还可以使用此方法将 top 命令的输出重定向到另一个程序,如下所示。
$ top -n1 -b | head
top - 16:58:36 up 246 days, 11:14, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 169 total, 1 running, 168 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 8306856k total, 7941612k used, 365244k free, 285144k buffers
Swap: 8385920k total, 104k used, 8385816k free, 7392088k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2064 592 512 S 0.0 0.0 0:02.24 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.47 migration/0
3 root 39 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)