定製儲存top輸出資訊的格式詳解

來源:互聯網
上載者:User

定製儲存top輸出資訊的格式詳解 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.44Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombieCpu(s): 2.4%us, 2.7%sy, 0.1%ni, 94.1%id, 0.4%wa, 0.0%hi, 0.3%si, 0.0%stMem: 1035140k total, 903792k used, 131348k free, 236160k buffersSwap: 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.38Tasks: 70 total, 2 running, 68 sleeping, 0 stopped, 0 zombieCpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%stMem: 1035140k total, 904040k used, 131100k free, 236368k buffersSwap: 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.32Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombieCpu0 : 0.1%us, 0.1%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.1%hi, 0.2%si, 0.0%stMem: 1035140k total, 904164k used, 130976k free, 236552k buffersSwap: 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:DefToggle 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.25Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombieCpu0 : 0.1%us, 0.2%sy, 0.5%ni, 98.7%id, 0.0%wa, 0.0%hi, 0.4%si, 0.0%stMem: 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.22Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0 zombieCpu0 : 2.4%us, 2.6%sy, 0.1%ni, 94.2%id, 0.4%wa, 0.0%hi, 0.3%si, 0.0%stMem: 1035140k total, 905032k used, 130108k free, 237004k buffersSwap: 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 ~]#這種方法對於一台機器來說比較容易,如果是批量部署的話還需拷貝檔案,就比較繁瑣了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.