批處理模式下運(yùn)行 top 命令的方法
top 命令 是每個(gè)人都在使用的用于 監(jiān)控 Linux 系統(tǒng)性能 的最好的命令。你可能已經(jīng)知道 top 命令的絕大部分操作,除了很少的幾個(gè)操作,如果我沒(méi)錯(cuò)的話,批處理模式就是其中之一。
大部分的腳本編寫(xiě)者和開(kāi)發(fā)人員都知道這個(gè),因?yàn)檫@個(gè)操作主要就是用來(lái)編寫(xiě)腳本。
如果你不了解這個(gè),不用擔(dān)心,我們將在這里介紹它。
什么是 top 命令的批處理模式
批處理模式允許你將 top 命令的輸出發(fā)送至其他程序或者文件中。
在這個(gè)模式中,top 命令將不會(huì)接收輸入并且持續(xù)運(yùn)行,直到迭代次數(shù)達(dá)到你用 -n 選項(xiàng)指定的次數(shù)為止。
如果你想解決 Linux 服務(wù)器上的任何性能問(wèn)題,你需要正確的 理解 top 命令的輸出。
1) 如何在批處理模式下運(yùn)行 top 命令
默認(rèn)地,top 命令按照 CPU 的使用率來(lái)排序輸出結(jié)果,所以當(dāng)你在批處理模式中運(yùn)行以下命令時(shí),它會(huì)執(zhí)行同樣的操作并打印前 35 行:
# top -bc | head -35 top - 06:41:14 up 8 days, 20:24, 1 user, load average: 0.87, 0.77, 0.81 Tasks: 139 total, 1 running, 136 sleeping, 0 stopped, 2 zombie %Cpu(s): 0.0 us, 3.2 sy, 0.0 ni, 96.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 3880940 total, 1595932 free, 886736 used, 1398272 buff/cache KiB Swap: 1048572 total, 514640 free, 533932 used. 2648472 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 191144 2800 1596 S 0.0 0.1 5:43.63 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 2 root 20 0 0 0 0 S 0.0 0.0 0:00.32 [kthreadd] 3 root 20 0 0 0 0 S 0.0 0.0 0:28.10 [ksoftirqd/0] 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:0H] 7 root rt 0 0 0 0 S 0.0 0.0 0:33.96 [migration/0] 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [rcu_bh] 9 root 20 0 0 0 0 S 0.0 0.0 63:05.12 [rcu_sched] 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-drain] 11 root rt 0 0 0 0 S 0.0 0.0 0:08.79 [watchdog/0] 12 root rt 0 0 0 0 S 0.0 0.0 0:08.82 [watchdog/1] 13 root rt 0 0 0 0 S 0.0 0.0 0:44.27 [migration/1] 14 root 20 0 0 0 0 S 0.0 0.0 1:22.45 [ksoftirqd/1] 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/1:0H] 18 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kdevtmpfs] 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [netns] 20 root 20 0 0 0 0 S 0.0 0.0 0:01.35 [khungtaskd] 21 root 0 -20 0 0 0 S 0.0 0.0 0:00.02 [writeback] 22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kintegrityd] 23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset] 24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kblockd] 25 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [md] 26 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [edac-poller] 33 root 20 0 0 0 0 S 0.0 0.0 1:19.07 [kswapd0] 34 root 25 5 0 0 0 S 0.0 0.0 0:00.00 [ksmd] 35 root 39 19 0 0 0 S 0.0 0.0 0:12.80 [khugepaged] 36 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [crypto] 44 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kthrotld] 46 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kmpath_rdacd]
2) 如何在批處理模式下運(yùn)行 top 命令并按內(nèi)存使用率排序結(jié)果
在批處理模式中運(yùn)行以下命令按內(nèi)存使用率對(duì)結(jié)果進(jìn)行排序:
# top -bc -o +%MEM | head -n 20 top - 06:42:00 up 8 days, 20:25, 1 user, load average: 0.66, 0.74, 0.80 Tasks: 146 total, 1 running, 145 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 KiB Mem : 3880940 total, 1422044 free, 1059176 used, 1399720 buff/cache KiB Swap: 1048572 total, 514640 free, 533932 used. 2475984 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 18105 mysql 20 0 1453900 156096 8816 S 0.0 4.0 2:12.98 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 1841 root 20 0 228980 107036 5360 S 0.0 2.8 0:05.56 /usr/local/cpanel/3rdparty/perl/528/bin/perl -T -w /usr/local/cpanel/3rdparty/bin/spamd --max-children=3 --max-spare=1 --allowed-ips=127.0.0.+ 4301 root 20 0 230208 104608 1816 S 0.0 2.7 0:03.77 spamd child 8139 nobody 20 0 257000 27108 3408 S 0.0 0.7 0:00.04 /usr/sbin/httpd -k start 7961 nobody 20 0 256988 26912 3160 S 0.0 0.7 0:00.05 /usr/sbin/httpd -k start 8190 nobody 20 0 256976 26812 3140 S 0.0 0.7 0:00.05 /usr/sbin/httpd -k start 8353 nobody 20 0 256976 26812 3144 S 0.0 0.7 0:00.04 /usr/sbin/httpd -k start 8629 nobody 20 0 256856 26736 3108 S 0.0 0.7 0:00.02 /usr/sbin/httpd -k start 8636 nobody 20 0 256856 26712 3100 S 0.0 0.7 0:00.03 /usr/sbin/httpd -k start 8611 nobody 20 0 256844 25764 2228 S 0.0 0.7 0:00.01 /usr/sbin/httpd -k start 8451 nobody 20 0 256844 25760 2220 S 0.0 0.7 0:00.04 /usr/sbin/httpd -k start 8610 nobody 20 0 256844 25748 2224 S 0.0 0.7 0:00.01 /usr/sbin/httpd -k start 8632 nobody 20 0 256844 25744 2216 S 0.0 0.7 0:00.03 /usr/sbin/httpd -k start
上面命令的詳細(xì)信息:
-b:批處理模式選項(xiàng)
-c:打印運(yùn)行中的進(jìn)程的絕對(duì)路徑
-o:指定進(jìn)行排序的字段
head:輸出文件的第一部分
-n:打印前 n 行
3) 如何在批處理模式下運(yùn)行 top 命令并按照指定的用戶(hù)進(jìn)程對(duì)結(jié)果進(jìn)行排序
如果你想要按照指定用戶(hù)進(jìn)程對(duì)結(jié)果進(jìn)行排序請(qǐng)運(yùn)行以下命令:
# top -bc -u mysql | head -n 10 top - 06:44:58 up 8 days, 20:27, 1 user, load average: 0.99, 0.87, 0.84 Tasks: 140 total, 1 running, 137 sleeping, 0 stopped, 2 zombie %Cpu(s): 13.3 us, 3.3 sy, 0.0 ni, 83.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 3880940 total, 1589832 free, 885648 used, 1405460 buff/cache KiB Swap: 1048572 total, 514640 free, 533932 used. 2649412 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 18105 mysql 20 0 1453900 156888 8816 S 0.0 4.0 2:16.42 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
4) 如何在批處理模式下運(yùn)行 top 命令并按照處理時(shí)間進(jìn)行排序
在批處理模式中使用以下 top 命令按照處理時(shí)間對(duì)結(jié)果進(jìn)行排序。這展示了任務(wù)從啟動(dòng)以來(lái)已使用的總 CPU 時(shí)間。
但是如果你想要檢查一個(gè)進(jìn)程在 Linux 上運(yùn)行了多長(zhǎng)時(shí)間請(qǐng)看接下來(lái)的文章:
檢查 Linux 中進(jìn)程運(yùn)行時(shí)間的五種方法
# top -bc -o TIME+ | head -n 20 top - 06:45:56 up 8 days, 20:28, 1 user, load average: 0.56, 0.77, 0.81 Tasks: 148 total, 1 running, 146 sleeping, 0 stopped, 1 zombie %Cpu(s): 0.0 us, 3.1 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 3880940 total, 1378664 free, 1094876 used, 1407400 buff/cache KiB Swap: 1048572 total, 514640 free, 533932 used. 2440332 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 9 root 20 0 0 0 0 S 0.0 0.0 63:05.70 [rcu_sched] 272 root 20 0 0 0 0 S 0.0 0.0 16:12.13 [xfsaild/vda1] 3882 root 20 0 229832 6212 1220 S 0.0 0.2 9:00.84 /usr/sbin/httpd -k start 1 root 20 0 191144 2800 1596 S 0.0 0.1 5:43.75 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 3761 root 20 0 68784 9820 2048 S 0.0 0.3 5:09.67 tailwatchd 3529 root 20 0 404380 3472 2604 S 0.0 0.1 3:24.98 /usr/sbin/rsyslogd -n 3520 root 20 0 574208 572 164 S 0.0 0.0 3:07.74 /usr/bin/python2 -Es /usr/sbin/tuned -l -P 444 dbus 20 0 58444 1144 612 S 0.0 0.0 2:23.90 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation 18105 mysql 20 0 1453900 157152 8816 S 0.0 4.0 2:17.29 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 249 root 0 -20 0 0 0 S 0.0 0.0 1:28.83 [kworker/0:1H] 14 root 20 0 0 0 0 S 0.0 0.0 1:22.46 [ksoftirqd/1] 33 root 20 0 0 0 0 S 0.0 0.0 1:19.07 [kswapd0] 342 root 20 0 39472 2940 2752 S 0.0 0.1 1:18.17 /usr/lib/systemd/systemd-journald
5) 如何在批處理模式下運(yùn)行 top 命令并將結(jié)果保存到文件中
如果出于解決問(wèn)題的目的,你想要和別人分享 top 命令的輸出,請(qǐng)使用以下命令重定向輸出到文件中:
# top -bc | head -35 > top-report.txt # cat top-report.txt top - 06:47:11 up 8 days, 20:30, 1 user, load average: 0.67, 0.77, 0.81 Tasks: 133 total, 4 running, 129 sleeping, 0 stopped, 0 zombie %Cpu(s): 59.4 us, 12.5 sy, 0.0 ni, 28.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 3880940 total, 1596268 free, 843284 used, 1441388 buff/cache KiB Swap: 1048572 total, 514640 free, 533932 used. 2659084 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 9686 daygeekc 20 0 406132 62184 43448 R 94.1 1.6 0:00.34 /opt/cpanel/ea-php56/root/usr/bin/php-cgi 9689 nobody 20 0 256588 24428 1184 S 5.9 0.6 0:00.01 /usr/sbin/httpd -k start 1 root 20 0 191144 2800 1596 S 0.0 0.1 5:43.79 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 2 root 20 0 0 0 0 S 0.0 0.0 0:00.32 [kthreadd] 3 root 20 0 0 0 0 S 0.0 0.0 0:28.11 [ksoftirqd/0] 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:0H] 7 root rt 0 0 0 0 S 0.0 0.0 0:33.96 [migration/0] 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [rcu_bh] 9 root 20 0 0 0 0 R 0.0 0.0 63:05.82 [rcu_sched] 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-drain] 11 root rt 0 0 0 0 S 0.0 0.0 0:08.79 [watchdog/0] 12 root rt 0 0 0 0 S 0.0 0.0 0:08.82 [watchdog/1] 13 root rt 0 0 0 0 S 0.0 0.0 0:44.28 [migration/1] 14 root 20 0 0 0 0 S 0.0 0.0 1:22.46 [ksoftirqd/1] 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/1:0H] 18 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kdevtmpfs] 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [netns] 20 root 20 0 0 0 0 S 0.0 0.0 0:01.35 [khungtaskd] 21 root 0 -20 0 0 0 S 0.0 0.0 0:00.02 [writeback] 22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kintegrityd] 23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset] 24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kblockd] 25 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [md] 26 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [edac-poller] 33 root 20 0 0 0 0 S 0.0 0.0 1:19.07 [kswapd0] 34 root 25 5 0 0 0 S 0.0 0.0 0:00.00 [ksmd] 35 root 39 19 0 0 0 S 0.0 0.0 0:12.80 [khugepaged] 36 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [crypto]
如何按照指定字段對(duì)結(jié)果進(jìn)行排序
在 top 命令的最新版本中, 按下 f 鍵進(jìn)入字段管理界面。
要使用新字段進(jìn)行排序, 請(qǐng)使用 up/down 箭頭選擇正確的選項(xiàng),然后再按下 s 鍵進(jìn)行排序。最后按 q 鍵退出此窗口。
Fields Management for window 1:Def, whose current sort field is %CPU Navigate with Up/Dn, Right selects for move then or Left commits, 'd' or toggles display, 's' sets sort. Use 'q' or to end! PID = Process Id nsUTS = UTS namespace Inode USER = Effective User Name LXC = LXC container name PR = Priority RSan = RES Anonymous (KiB) NI = Nice Value RSfd = RES File-based (KiB) VIRT = Virtual Image (KiB) RSlk = RES Locked (KiB) RES = Resident Size (KiB) RSsh = RES Shared (KiB) SHR = Shared Memory (KiB) CGNAME = Control Group name S = Process Status NU = Last Used NUMA node %CPU = CPU Usage %MEM = Memory Usage (RES) TIME+ = CPU Time, hundredths COMMAND = Command Name/Line PPID = Parent Process pid UID = Effective User Id RUID = Real User Id RUSER = Real User Name SUID = Saved User Id SUSER = Saved User Name GID = Group Id GROUP = Group Name PGRP = Process Group Id TTY = Controlling Tty TPGID = Tty Process Grp Id SID = Session Id nTH = Number of Threads P = Last Used Cpu (SMP) TIME = CPU Time SWAP = Swapped Size (KiB) CODE = Code Size (KiB) DATA = Data+Stack (KiB) nMaj = Major Page Faults nMin = Minor Page Faults nDRT = Dirty Pages Count WCHAN = Sleeping in Function Flags = Task Flags CGROUPS = Control Groups SUPGIDS = Supp Groups IDs SUPGRPS = Supp Groups Names TGID = Thread Group Id OOMa = OOMEM Adjustment OOMs = OOMEM Score current ENVIRON = Environment vars vMj = Major Faults delta vMn = Minor Faults delta USED = Res+Swap Size (KiB) nsIPC = IPC namespace Inode nsMNT = MNT namespace Inode nsNET = NET namespace Inode nsPID = PID namespace Inode nsUSER = USER namespace Inode
對(duì) top 命令的舊版本,請(qǐng)按 shift+f 或 shift+o 鍵進(jìn)入字段管理界面進(jìn)行排序。
要使用新字段進(jìn)行排序,請(qǐng)選擇相應(yīng)的排序字段字母, 然后按下回車(chē)鍵排序。
Current Sort Field: N for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id b: PPID = Parent Process Pid c: RUSER = Real user name d: UID = User Id e: USER = User Name f: GROUP = Group Name g: TTY = Controlling Tty h: PR = Priority i: NI = Nice value j: P = Last used cpu (SMP) k: %CPU = CPU usage l: TIME = CPU Time m: TIME+ = CPU Time, hundredths * N: %MEM = Memory usage (RES) o: VIRT = Virtual Image (kb) p: SWAP = Swapped size (kb) q: RES = Resident size (kb) r: CODE = Code size (kb) s: DATA = Data+Stack size (kb) t: SHR = Shared Mem size (kb) u: nFLT = Page Fault count v: nDRT = Dirty Pages count w: S = Process Status x: COMMAND = Command name/line y: WCHAN = Sleeping in Function z: Flags = Task Flags Note1: If a selected sort field can't be shown due to screen width or your field order, the '<' and '>' keys will be unavailable until a field within viewable range is chosen. Note2: Field sorting uses internal values, not those in column display. Thus, the TTY & WCHAN fields will violate strict ASCII collating sequence. (shame on you if WCHAN is chosen)
總結(jié)
以上所述是小編給大家介紹的批處理模式下運(yùn)行 top 命令的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
VirtualBox 未指定要bridged的網(wǎng)絡(luò)界面的解決辦法
這篇文章主要介紹了VirtualBox 未指定要bridged的網(wǎng)絡(luò)界面的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家解決遇到這樣的問(wèn)題,需要的朋友可以參考下2017-10-10CentOS7設(shè)置ssh服務(wù)以及端口修改方式
這篇文章主要介紹了CentOS7設(shè)置ssh服務(wù)以及端口修改方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Linux、CentOS下安裝zip與unzip指令功能(服務(wù)器)
這篇文章主要介紹了Linux、CentOS下安裝zip與unzip指令的操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-11-11