top命令的重要性和使用方法不多说了,这里终点讨论如何保存top命令的输出信息。
保存top命令的输出到一个文件的方法是:top -n1b > topinfo.txt,这没什么好奇的,但是输出的信息的格式都是默认的。
- [root@localhost ~]# top -n1b > topinfo.txt
- [root@localhost ~]# cat topinfo.txt
- top - 14:14:18 up 17:49, 3 users, load average: 0.00, 0.02, 0.44
- Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombie
- Cpu(s): 2.4%us, 2.7%sy, 0.1%ni, 94.1%id, 0.4%wa, 0.0%hi, 0.3%si, 0.0%st
- Mem: 1035140k total, 903792k used, 131348k free, 236160k buffers
- Swap: 2097144k total, 0k used, 2097144k free, 545312k cached
- PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
- 1 root 15 0 2072 588 504 S 0.0 0.1 0:00.97 init
- 2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
- 3 root 39 19 0 0 0 S 0.0 0.0 0:00.07 ksoftirqd/0
- 4 root RT -5 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
- 5 root 10 -5 0 0 0 S 0.0 0.0 0:00.20 events/0
- 6 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khelper
- 7 root 12 -5 0 0 0 S 0.0 0.0 0:00.05 kthread
- 10 root 10 -5 0 0 0 S 0.0 0.0 0:00.65 kblockd/0
- 11 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid
- 179 root 18 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0
- 182 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khubd
- 184 root 19 -5 0 0 0 S 0.0 0.0 0:00.00 kseriod
- 249 root 15 0 0 0 0 S 0.0 0.0 0:00.10 pdflush
- 250 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kswapd0
- 251 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 aio/0
- 473 root 11 -5 0 0 0 S 0.0 0.0 0:00.00 kpsmoused
- 497 root 16 -5 0 0 0 S 0.0 0.0 0:00.00 ata/0
- 498 root 16 -5 0 0 0 S 0.0 0.0 0:00.00 ata_aux
- 509 root 10 -5 0 0 0 S 0.0 0.0 0:00.08 mpt_poll_0
- 510 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_0
- 513 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kstriped
- 522 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 ksnapd
- 533 root 10 -5 0 0 0 S 0.0 0.0 0:07.86 kjournald
- 564 root 11 -5 0 0 0 S 0.0 0.0 0:00.00 kauditd
- 597 root 21 -4 3080 1640 492 S 0.0 0.2 0:00.60 udevd
- 1290 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kgameportd
- 2170 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kmpathd/0
- 2171 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kmpath_handlerd
- 2195 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kjournald
- 2416 root 18 0 2168 512 396 S 0.0 0.0 0:00.23 mcstransd
- 2648 root 15 0 2316 712 412 S 0.0 0.1 0:00.07 dhclient
- 2805 root 16 0 11584 9952 240 S 0.0 1.0 0:00.01 restorecond
- 2819 root 18 0 1728 576 480 S 0.0 0.1 0:00.03 syslogd
- 2822 root 15 0 1680 392 328 S 0.0 0.0 0:00.00 klogd
- 2861 dbus 18 0 12996 1072 844 S 0.0 0.1 0:00.04 dbus-daemon
- 2907 root 24 0 12728 1356 604 S 0.0 0.1 0:02.66 pcscd
- 2921 haldaemo 15 0 6464 4536 1688 S 0.0 0.4 0:03.72 hald
- 2922 root 18 0 3156 1076 916 S 0.0 0.1 0:00.00 hald-runner
- 2930 haldaemo 18 0 2020 812 716 S 0.0 0.1 0:00.00 hald-addon-acpi
- 2943 haldaemo 15 0 2020 820 720 S 0.0 0.1 0:00.00 hald-addon-keyb
- 2952 root 18 0 1976 768 684 S 0.0 0.1 0:23.22 hald-addon-stor
- 2985 root 25 0 1916 452 372 S 0.0 0.0 0:00.00 hidd
- 3010 root 18 0 103m 792 544 S 0.0 0.1 0:00.04 tgtd
- 3011 root 18 0 3304 340 140 S 0.0 0.0 0:00.18 tgtd
- 3049 root 24 0 5140 764 520 S 0.0 0.1 0:00.00 hpiod
- 3054 root 15 0 13444 4808 1020 S 0.0 0.5 0:00.16 python
- 3072 root 15 0 7068 1064 668 S 0.0 0.1 0:00.01 sshd
- 3193 root 15 0 1900 472 392 S 0.0 0.0 0:00.00 gpm
- 3207 root 18 0 5296 1184 648 S 0.0 0.1 0:00.11 crond
- 3221 root 18 0 14436 2216 1420 S 0.0 0.2 0:00.01 smbd
- 3224 root 15 0 9408 1368 912 S 0.0 0.1 0:00.28 nmbd
- 3265 root 18 0 14436 1076 280 S 0.0 0.1 0:00.00 smbd
- 3276 root 18 0 3508 464 284 S 0.0 0.0 0:00.00 smartd
- 3280 root 16 0 2892 1304 996 S 0.0 0.1 0:00.12 login
- 3281 root 16 0 1664 440 384 S 0.0 0.0 0:00.00 mingetty
- 3284 root 20 0 1664 444 384 S 0.0 0.0 0:00.00 mingetty
- 3289 root 18 0 1664 444 384 S 0.0 0.0 0:00.00 mingetty
- 3297 root 18 0 1664 448 384 S 0.0 0.0 0:00.00 mingetty
- 3300 root 18 0 1664 448 384 S 0.0 0.0 0:00.00 mingetty
- 3336 root 34 19 25664 10m 2152 S 0.0 1.0 0:00.15 yum-updatesd
- 3338 root 34 19 2560 1184 984 S 0.0 0.1 0:00.32 gam_server
- 3339 root 17 0 4632 1492 1224 S 0.0 0.1 0:00.03 bash
- 7387 root 15 0 0 0 0 S 0.0 0.0 0:00.20 pdflush
- 9219 root 25 0 4536 1260 1100 S 0.0 0.1 0:00.00 mysqld_safe
- 9269 mysql 15 0 134m 18m 4488 S 0.0 1.8 0:01.26 mysqld
- 10548 root 15 0 9924 2856 2296 S 0.0 0.3 0:00.42 sshd
- 10550 root 15 0 4640 1452 1184 S 0.0 0.1 0:00.03 bash
- 10684 root 18 0 10084 2868 2296 S 0.0 0.3 0:00.52 sshd
- 10686 root 15 0 4640 1504 1228 S 0.0 0.1 0:00.05 bash
- 11432 root 15 0 2200 872 692 R 0.0 0.1 0:00.00 top
- [root@localhost ~]#
如何修改这些默认的设置呢?比如输出结果中的进程部分只包含cmd、pid、%cpu、%mem等等。使用脚本比较繁琐而且有些也是无法实现的,比如保存的信息里有每颗cpu的负载信息。
下面是修改top -n1b输出的默认设置的步骤:
第一步:执行top命令
- top - 14:16:56 up 17:52, 3 users, load average: 0.07, 0.02, 0.38
- Tasks: 70 total, 2 running, 68 sleeping, 0 stopped, 0 zombie
- Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
- Mem: 1035140k total, 904040k used, 131100k free, 236368k buffers
- Swap: 2097144k total, 0k used, 2097144k free, 545320k cached
- PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
- 11434 root 18 0 2204 988 804 R 0.3 0.1 0:00.04 top
- 1 root 15 0 2072 588 504 S 0.0 0.1 0:00.97 init
- 2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
- 3 root 34 19 0 0 0 S 0.0 0.0 0:00.07 ksoftirqd/0
- 4 root RT -5 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
- 5 root 10 -5 0 0 0 S 0.0 0.0 0:00.20 events/0
- 6 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khelper
- 7 root 12 -5 0 0 0 S 0.0 0.0 0:00.05 kthread
- 10 root 10 -5 0 0 0 S 0.0 0.0 0:00.65 kblockd/0
- 11 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid
- 179 root 18 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0
- 182 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khubd
- 184 root 19 -5 0 0 0 S 0.0 0.0 0:00.00 kseriod
- 249 root 15 0 0 0 0 S 0.0 0.0 0:00.10 pdflush
- 250 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kswapd0
- 251 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 aio/0
第二步:修改输出格式,我这里演示:显示每颗cpu的信息,和进程部分只显示cmd、pid、%cpu、%mem。
按1
- top - 14:19:11 up 17:54, 3 users, load average: 0.00, 0.01, 0.32
- Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombie
- Cpu0 : 0.1%us, 0.1%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.1%hi, 0.2%si, 0.0%st
- Mem: 1035140k total, 904164k used, 130976k free, 236552k buffers
- Swap: 2097144k total, 0k used, 2097144k free, 545320k cached
- PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
- 11434 root 18 0 2204 988 804 R 0.3 0.1 0:00.22 top
- 1 root 15 0 2072 588 504 S 0.0 0.1 0:00.97 init
- 2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
- 3 root 34 19 0 0 0 S 0.0 0.0 0:00.07 ksoftirqd/0
- 4 root RT -5 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
- 5 root 10 -5 0 0 0 S 0.0 0.0 0:00.20 events/0
- 6 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khelper
- 7 root 12 -5 0 0 0 S 0.0 0.0 0:00.05 kthread
- 10 root 10 -5 0 0 0 S 0.0 0.0 0:00.65 kblockd/0
- 11 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid
- 179 root 18 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0
- 182 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khubd
- 184 root 19 -5 0 0 0 S 0.0 0.0 0:00.00 kseriod
- 249 root 15 0 0 0 0 S 0.0 0.0 0:00.10 pdflush
- 250 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kswapd0
- 251 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 aio/0
按f:
- Current Fields: AEHIOQTWKNMbcdfgjplrsuvyzX for window 1:Def
- Toggle fields via field letter, type any other key to return
- * A: PID = Process Id s: DATA = Data+Stack size (kb) 0x00100000 PF_USEDFPU (thru 2.4)
- * H: PR = Priority v: nDRT = Dirty Pages count
- * I: NI = Nice value y: WCHAN = Sleeping in Function
- * O: VIRT = Virtual Image (kb) z: Flags = Task Flags <sched.h>
- * Q: RES = Resident size (kb) * X: COMMAND = Command name/line
- * T: SHR = Shared Mem size (kb)
- * W: S = Process Status Flags field:
- * K: %CPU = CPU usage 0x00000001 PF_ALIGNWARN
- * N: %MEM = Memory usage (RES) 0x00000002 PF_STARTING
- * M: TIME+ = CPU Time, hundredths 0x00000004 PF_EXITING
- b: PPID = Parent Process Pid 0x00000040 PF_FORKNOEXEC
- c: RUSER = Real user name 0x00000100 PF_SUPERPRIV
- d: UID = User Id 0x00000200 PF_DUMPCORE
- f: GROUP = Group Name 0x00000400 PF_SIGNALED
- g: TTY = Controlling Tty 0x00000800 PF_MEMALLOC
- j: P = Last used cpu (SMP) 0x00002000 PF_FREE_PAGES (2.5)
- p: SWAP = Swapped size (kb) 0x00008000 debug flag (2.5)
- l: TIME = CPU Time 0x00024000 special threads (2.5)
- r: CODE = Code size (kb) 0x001D0000 special states (2.5)
定制输出信息,按A、H、I、O、Q、T、W去掉不需要的信息:
- top - 14:22:29 up 17:58, 3 users, load average: 0.00, 0.00, 0.25
- Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombie
- Cpu0 : 0.1%us, 0.2%sy, 0.5%ni, 98.7%id, 0.0%wa, 0.0%hi, 0.4%si, 0.0%st
- Mem: 1035140k total, 905032k used, 130108k free, 236828k buffersSwap: 2097144k total, 0k used, 2097144k free, 545320k cached
- USER %CPU %MEM TIME+ COMMAND
- root 0.0 0.1 0:00.97 init
- root 0.0 0.0 0:00.00 migration/0
- root 0.0 0.0 0:00.07 ksoftirqd/0
- root 0.0 0.0 0:00.01 watchdog/0
- root 0.0 0.0 0:00.21 events/0
- root 0.0 0.0 0:00.00 khelper
- root 0.0 0.0 0:00.05 kthread
- root 0.0 0.0 0:00.65 kblockd/0
- root 0.0 0.0 0:00.00 kacpid
- root 0.0 0.0 0:00.00 cqueue/0
- root 0.0 0.0 0:00.00 khubd
- root 0.0 0.0 0:00.00 kseriod
- root 0.0 0.0 0:00.10 pdflush
- root 0.0 0.0 0:00.00 kswapd0
- root 0.0 0.0 0:00.00 aio/0
- root 0.0 0.0 0:00.00 kpsmoused
按W,保存设置,在~/.toprc。
按q,退出top命令
现在再执行top -n1b试试结果吧:
- [root@localhost ~]# top -n1b > topinfo.txt
- [root@localhost ~]# cat topinfo.txt
- top - 14:24:37 up 18:00, 3 users, load average: 0.00, 0.00, 0.22
- Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombie
- Cpu0 : 2.4%us, 2.6%sy, 0.1%ni, 94.2%id, 0.4%wa, 0.0%hi, 0.3%si, 0.0%st
- Mem: 1035140k total, 905032k used, 130108k free, 237004k buffers
- Swap: 2097144k total, 0k used, 2097144k free, 545316k cached
- USER %CPU %MEM TIME+ COMMAND
- root 0.0 0.1 0:00.97 init
- root 0.0 0.0 0:00.00 migration/0
- root 0.0 0.0 0:00.07 ksoftirqd/0
- root 0.0 0.0 0:00.01 watchdog/0
- root 0.0 0.0 0:00.21 events/0
- root 0.0 0.0 0:00.00 khelper
- root 0.0 0.0 0:00.05 kthread
- root 0.0 0.0 0:00.65 kblockd/0
- root 0.0 0.0 0:00.00 kacpid
- root 0.0 0.0 0:00.00 cqueue/0
- root 0.0 0.0 0:00.00 khubd
- root 0.0 0.0 0:00.00 kseriod
- root 0.0 0.0 0:00.10 pdflush
- root 0.0 0.0 0:00.00 kswapd0
- root 0.0 0.0 0:00.00 aio/0
- root 0.0 0.0 0:00.00 kpsmoused
- root 0.0 0.0 0:00.00 ata/0
- root 0.0 0.0 0:00.00 ata_aux
- root 0.0 0.0 0:00.08 mpt_poll_0
- root 0.0 0.0 0:00.00 scsi_eh_0
- root 0.0 0.0 0:00.00 kstriped
- root 0.0 0.0 0:00.00 ksnapd
- root 0.0 0.0 0:07.93 kjournald
- root 0.0 0.0 0:00.00 kauditd
- root 0.0 0.2 0:00.60 udevd
- root 0.0 0.0 0:00.00 kgameportd
- root 0.0 0.0 0:00.00 kmpathd/0
- root 0.0 0.0 0:00.00 kmpath_handlerd
- root 0.0 0.0 0:00.00 kjournald
- root 0.0 0.0 0:00.23 mcstransd
- root 0.0 0.1 0:00.07 dhclient
- root 0.0 1.0 0:00.01 restorecond
- root 0.0 0.1 0:00.03 syslogd
- root 0.0 0.0 0:00.00 klogd
- dbus 0.0 0.1 0:00.04 dbus-daemon
- root 0.0 0.1 0:02.69 pcscd
- haldaemo 0.0 0.4 0:03.74 hald
- root 0.0 0.1 0:00.00 hald-runner
- haldaemo 0.0 0.1 0:00.00 hald-addon-acpi
- haldaemo 0.0 0.1 0:00.00 hald-addon-keyb
- root 0.0 0.1 0:23.44 hald-addon-stor
- root 0.0 0.0 0:00.00 hidd
- root 0.0 0.1 0:00.04 tgtd
- root 0.0 0.0 0:00.18 tgtd
- root 0.0 0.1 0:00.00 hpiod
- root 0.0 0.5 0:00.16 python
- root 0.0 0.1 0:00.01 sshd
- root 0.0 0.0 0:00.00 gpm
- root 0.0 0.1 0:00.11 crond
- root 0.0 0.2 0:00.01 smbd
- root 0.0 0.1 0:00.29 nmbd
- root 0.0 0.1 0:00.00 smbd
- root 0.0 0.0 0:00.00 smartd
- root 0.0 0.1 0:00.12 login
- root 0.0 0.0 0:00.00 mingetty
- root 0.0 0.0 0:00.00 mingetty
- root 0.0 0.0 0:00.00 mingetty
- root 0.0 0.0 0:00.00 mingetty
- root 0.0 0.0 0:00.00 mingetty
- root 0.0 1.0 0:00.16 yum-updatesd
- root 0.0 0.1 0:00.32 gam_server
- root 0.0 0.1 0:00.03 bash
- root 0.0 0.0 0:00.20 pdflush
- root 0.0 0.1 0:00.00 mysqld_safe
- mysql 0.0 1.8 0:01.27 mysqld
- root 0.0 0.3 0:00.60 sshd
- root 0.0 0.1 0:00.04 bash
- root 0.0 0.3 0:00.52 sshd
- root 0.0 0.1 0:00.05 bash
- root 0.0 0.1 0:00.00 top
- [root@localhost ~]#