«

»


21
2011

将top命令显示内容输出至文本文件

习惯于使用top命令检查Linux系统的当前资源使用情况,但是如何将其输出至文件呢?首先想到的肯定是管道重定向,使用大于符号>,试试看:

[michael@wardking.com ~]$ top -n 1 > top-output

[michael@wardking.com ~]$ less top-output
“top-output” may be a binary file.  See it anyway?

直接输出的结果是二进制文件?没错!看来得另想办法,遇到问题先读manual,这是学习Linux必须养成的习惯。仔细阅读会发现以下两项:

-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.

看来选项-b是关键,在top命令后加上后试试

[michael@wardking.com ~]$ top -n 1 -b > top-output

[michael@wardking.com ~]$ less top-output
top – 11:44:32 up 8 days,  1:53,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 131 total,   1 running, 130 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.0%sy,  0.0%ni, 99.7%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4046052k total,  3807064k used,   238988k free,   278240k buffers
Swap:  2031608k total,      140k used,  2031468k free,  2733024k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
28280 root      15   0 12760  984  720 R  2.0  0.0   0:00.01 top
    1 root      15   0 10372  692  580 S  0.0  0.0   0:00.90 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.56 migration/0
    3 root      34  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.01 migration/1
    6 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1

输出的结果正是所需要的!

接下来做进一步思考,既然可以按照此方法输出至文本文件,也可以使用管道直接输出为其它命令的参数,比如:top -n 1 | less,或者top -n 1 -b | grep -e httpd -e COMMAND

Permanent link to this article: http://www.wardking.com/2011/08/howto-capture-linux-top-command-output-to-text-file/

1 条评论

  1. 雷必斯 says:

    很好的网站支持一下你

发表评论

呢称和邮箱是必填项,您的邮箱只有管理员可见。

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>