Linux命令行工具 - top
top命令能夠?qū)崟r(shí)顯示系統(tǒng)中各個(gè)進(jìn)程的資源占用狀況,類(lèi)似于Windows的任務(wù)管理器.
語(yǔ)法說(shuō)明
[root@icloud-store ~]# top -h
procps-ng version 3.3.10
Usage:
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
示例應(yīng)用
查看當(dāng)前系統(tǒng)各個(gè)進(jìn)程的資源占用狀況.
[root@icloud-store ~]# top
top - 09:30:03 up 4 days, 2:39, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 82 total, 1 running, 81 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.7 us, 0.4 sy, 0.0 ni, 98.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1866900 total, 87844 free, 1458564 used, 320492 buff/cache
KiB Swap: 2096444 total, 741980 free, 1354464 used. 150432 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 43240 2568 1568 S 0.0 0.1 0:04.88 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:02.35 ksoftirqd/0
6 root 20 0 0 0 0 S 0.0 0.0 0:02.07 kworker/u256:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 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 0:12.25 rcu_sched
10 root rt 0 0 0 0 S 0.0 0.0 0:01.52 watchdog/0
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
14 root 20 0 0 0 0 S 0.0 0.0 0:00.07 khungtaskd
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
25 root 20 0 0 0 0 S 0.0 0.0 0:02.89 kswapd0
26 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
27 root 39 19 0 0 0 S 0.0 0.0 0:01.04 khugepaged
28 root 20 0 0 0 0 S 0.0 0.0 0:00.00 fsnotify_mark
29 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
37 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld
38 root 20 0 0 0 0 S 0.0 0.0 0:01.67 kworker/u256:1
39 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kmpath_rdacd
40 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kpsmoused
41 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ipv6_addrconf
60 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 deferwq
92 root 20 0 0 0 0 S 0.0 0.0 0:01.48 kauditd
223 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
235 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_0
結(jié)果分析
統(tǒng)計(jì)信息區(qū)前五行是系統(tǒng)整體的統(tǒng)計(jì)信息.
第一行 - 系統(tǒng)負(fù)載
[root@icloud-store ~]# uptime
09:32:00 up 4 days, 2:41, 2 users, load average: 0.00, 0.01, 0.05
Load Average是什么
linux系統(tǒng)中的Load對(duì)當(dāng)前CPU工作量的度量,簡(jiǎn)單的說(shuō)是進(jìn)程隊(duì)列的長(zhǎng)度;Load Average的值就是一段時(shí)間(1分鐘、5分鐘、15分鐘)內(nèi)平均Load.
假設(shè)我們的系統(tǒng)是單CPU單內(nèi)核的,把它比喻成是一條單向馬路,把CPU任務(wù)比作汽車(chē).當(dāng)車(chē)不多的時(shí)候,load <1;當(dāng)車(chē)占滿(mǎn)整個(gè)馬路的時(shí)候 load=1;當(dāng)馬路都站滿(mǎn)了,而且馬路外還堆滿(mǎn)了汽車(chē)的時(shí)候 load>1.
我們經(jīng)常會(huì)發(fā)現(xiàn)服務(wù)器Load > 1但是運(yùn)行仍然不錯(cuò),那是因?yàn)榉?wù)器是多核處理器(Multi-core),假設(shè)我們服務(wù)器CPU是2核,那么將意味我們擁有2條馬路,我們的Load = 2時(shí),所有馬路都跑滿(mǎn)車(chē)輛.
Load Average警惕閾值
- 0.7 < load < 1: 此時(shí)是不錯(cuò)的狀態(tài),如果進(jìn)來(lái)更多的汽車(chē),你的馬路仍然可以應(yīng)付.
- load = 1: 你的馬路即將擁堵,而且沒(méi)有更多的資源額外的任務(wù),趕緊看看發(fā)生了什么吧.
- load > 5: 非常嚴(yán)重?fù)矶?我們的馬路非常繁忙,每輛車(chē)都無(wú)法很快的運(yùn)行
第二行 - 進(jìn)程信息
Tasks: 82 total, 1 running, 81 sleeping, 0 stopped, 0 zombie
- total : 進(jìn)程總數(shù)
- running : 正在運(yùn)行的進(jìn)程數(shù)
- sleeping : 睡眠的進(jìn)程數(shù)
- stopped : 停止的進(jìn)程數(shù)
- zombie : 僵尸進(jìn)程數(shù)
第三行 - CPU狀態(tài)
%Cpu(s): 0.7 us, 0.4 sy, 0.0 ni, 98.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
- us:用戶(hù)空間占用CPU百分比
- sy:內(nèi)核空間占用CPU百分比
- ni:用戶(hù)進(jìn)程空間內(nèi)改變過(guò)優(yōu)先級(jí)的進(jìn)程占用CPU百分比
- id :空閑CPU百分比
- wa : 等待輸入輸出的CPU時(shí)間百分比
- hi:硬中斷(Hardware IRQ)占用CPU的百分比
- si:軟中斷(Software Interrupts)占用CPU的百分比
- st:虛擬機(jī)占用百分比
第四行 - 內(nèi)存狀態(tài)
KiB Mem : 1866900 total, 87844 free, 1458564 used, 320492 buff/cache
- total: 物理內(nèi)存總量
- free: 空閑內(nèi)存總量
- used: 已使用的物理內(nèi)存總量
- buff/cache: 用作緩存的內(nèi)存量
第五行 - swap交換分區(qū)
KiB Swap: 2096444 total, 741980 free, 1354464 used. 150432 avail Mem
- total: 交換區(qū)總量
- free: 空閑交換區(qū)總量
- used: 已使用的交換區(qū)總量
- avail Mem: 可用內(nèi)存
第六行(+) - 進(jìn)程統(tǒng)計(jì)信息
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 43240 2568 1568 S 0.0 0.1 0:04.88 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
- PID: 進(jìn)程編號(hào)
- USER: 所屬用戶(hù)
- PR: 優(yōu)先級(jí)
- NI: nice值,負(fù)值表示高優(yōu)先級(jí),正值表示低優(yōu)先級(jí)
- VIRT: 進(jìn)程使用的虛擬內(nèi)存總量,單位kb.VIRT=SWAP+RES
- RES: 進(jìn)程使用的、未被換出的物理內(nèi)存大小,單位kb.RES=CODE+DATA
- SHR: 共享內(nèi)存大小,單位kb
- S: 進(jìn)程狀態(tài)(D=不可中斷的睡眠狀態(tài),R=運(yùn)行,S=睡眠,T=跟蹤/停止,Z=僵尸進(jìn)程)
- %CPU:上次更新到現(xiàn)在的CPU時(shí)間占用百分比
- %MEM: 進(jìn)程使用的物理內(nèi)存百分比
- TIME+: 進(jìn)程使用的CPU時(shí)間總計(jì),單位1/100秒
- COMMAND: 命令名/命令行
進(jìn)程內(nèi)線(xiàn)程資源消耗排查
查看某個(gè)進(jìn)程下的線(xiàn)程的統(tǒng)計(jì)信息
[root@icloud-store ~]# top -Hp 21109
top - 11:08:00 up 4 days, 4:17, 1 user, load average: 0.00, 0.01, 0.05
Threads: 32 total, 0 running, 32 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 : 1866900 total, 96376 free, 1452020 used, 318504 buff/cache
KiB Swap: 2096444 total, 741980 free, 1354464 used. 153668 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
21109 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 java
21137 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:04.71 java
21138 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 GC Thread#0
21139 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:05.67 CMS Main Thread
21140 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:09.73 VM Thread
21141 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 Reference Handl
21142 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 Finalizer
21143 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 Signal Dispatch
21144 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:12.27 C2 CompilerThre
21145 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:02.97 C1 CompilerThre
21146 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.04 Sweeper thread
21147 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.22 Common-Cleaner
21148 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 Service Thread
21149 elon 20 0 4471136 0.986g 0 S 0.0 55.4 3:15.52 VM Periodic Tas
21152 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:50.20 elasticsearch[q
21171 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:06.02 elasticsearch[q
21172 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:10.63 threadDeathWatc
21173 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.28 elasticsearch[q
21174 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.25 elasticsearch[q
21182 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:02.21 elasticsearch[q
21183 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:06.95 elasticsearch[q
21184 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:00.00 elasticsearch[k
21650 elon 20 0 4471136 0.986g 0 S 0.0 55.4 0:05.45 elasticsearch[q
top內(nèi)交互命令
在top命令執(zhí)行后,可以使用下面這些些交互命令進(jìn)行top的內(nèi)監(jiān)控查看或者設(shè)置.
- h或者?: 顯示幫助畫(huà)面,給出一些簡(jiǎn)短的命令總結(jié)說(shuō)明.
- k: 終止一個(gè)進(jìn)程;系統(tǒng)將提示用戶(hù)輸入需要終止的進(jìn)程PID,以及需要發(fā)送給該進(jìn)程什么樣的信號(hào).一般的終止進(jìn)程可以使用15信號(hào);如果不能正常結(jié)束那就使用信號(hào)9強(qiáng)制結(jié)束該進(jìn)程.默認(rèn)值是信號(hào)15.在安全模式中此命令被屏蔽.
- i: 忽略閑置和僵死進(jìn)程.這是一個(gè)開(kāi)關(guān)式命令.
- q: 退出程序.
- r: 重新安排一個(gè)進(jìn)程的優(yōu)先級(jí)別.系統(tǒng)提示用戶(hù)輸入需要改變的進(jìn)程PID以及需要設(shè)置的進(jìn)程優(yōu)先級(jí)值.輸入一個(gè)正值將使優(yōu)先級(jí)降低,反之則可以使該進(jìn)程擁有更高的優(yōu)先權(quán).默認(rèn)值是10.
- S: 切換到累計(jì)模式.
- s: 改變兩次刷新之間的延遲時(shí)間.系統(tǒng)將提示用戶(hù)輸入新的時(shí)間,單位為s.如果有小數(shù),就換算成m s.輸入0值則系統(tǒng)將不斷刷新,默認(rèn)值是5 s.需要注意的是如果設(shè)置太小的時(shí)間,很可能會(huì)引起不斷刷新,從而根本來(lái)不及看清顯示的情況,而且系統(tǒng)負(fù)載也會(huì)大大增加.
- f或者F: 從當(dāng)前顯示中添加或者刪除項(xiàng)目.
- o或者O: 改變顯示項(xiàng)目的順序.
- l: 切換顯示平均負(fù)載和啟動(dòng)時(shí)間信息.
- m: 切換顯示內(nèi)存信息.
- t: 切換顯示進(jìn)程和CPU狀態(tài)信息.
- c: 切換顯示命令名稱(chēng)和完整命令行.
- M: 根據(jù)駐留內(nèi)存大小進(jìn)行排序.
- P: 根據(jù)CPU使用百分比大小進(jìn)行排序.
- T: 根據(jù)時(shí)間/累計(jì)時(shí)間進(jìn)行排序.
- W: 將當(dāng)前設(shè)置寫(xiě)入~/.toprc文件中.這是寫(xiě)top配置文件的推薦方法.
查看當(dāng)前內(nèi)存信息
查看占用內(nèi)存最多的K個(gè)進(jìn)程
top命令(然后按下M,注意大寫(xiě)) 或
ps -aux | sort -k4nr | head -K, 如果是10個(gè)進(jìn)程K=10
查看占用CPU最多的K個(gè)進(jìn)程
top命令(然后按下P,注意大寫(xiě)) 或
ps -aux | sort -k3nr | head -K
參數(shù)解讀
- ps -aux中(a指代all—所有的進(jìn)程,u指代userid,既執(zhí)行該進(jìn)程的用戶(hù)id,x指代顯示所有程序,不以終端機(jī)來(lái)區(qū)分).
- sort -k4nr中(k代表從第幾個(gè)位置開(kāi)始,后面的數(shù)字4即是其開(kāi)始位置,結(jié)束位置如果沒(méi)有,則默認(rèn)到最后;n指代numberic sort,根據(jù)其數(shù)值排序;r指代reverse這里是指反向比較結(jié)果,輸出時(shí)默認(rèn)從小到大,反向后從大到小);本例中可以看到%MEM在第4個(gè)位置,根據(jù)%MEM的數(shù)值進(jìn)行由大到小的排序.
- head -K(K指代行數(shù),即輸出前幾位的結(jié)果)
更高級(jí)的top
htop
與Linux傳統(tǒng)的top相比,htop更加人性化.它可讓用戶(hù)交互式操作,支持顏色主題,可橫向或縱向滾動(dòng)瀏覽進(jìn)程列表,并支持鼠標(biāo)操作.
[root@icloud-store ~]# htop
CPU[ 0.0%] Tasks: 31, 83 thr; 1 running
Mem[|||||||||||||||||||||||||||||||||||||||||1.50G/1.78G] Load average: 0.00 0.01 0.05
Swp[|||||||||||||||||||||||||||||||||| 1.29G/2.00G] Uptime: 4 days, 04:00:37
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
120391 root 20 0 119M 2332 1460 R 0.7 0.1 0:00.04 htop
342 root 20 0 45492 8656 8432 S 0.0 0.5 0:58.31 /usr/lib/systemd/systemd-journald
21109 elon 20 0 4366M 1009M 0 S 0.0 55.4 6:54.92 /usr/local/jdk-9.0.1/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkS
21155 elon 20 0 4366M 1009M 0 S 0.0 55.4 0:06.63 /usr/local/jdk-9.0.1/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkS
21149 elon 20 0 4366M 1009M 0 S 0.0 55.4 3:14.67 /usr/local/jdk-9.0.1/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkS
668 root 20 0 487M 14824 7712 S 0.0 0.8 4:46.85 /usr/bin/python2 -s /usr/bin/fail2ban-server -s /var/run/fail
1 root 20 0 43240 2568 1568 S 0.0 0.1 0:04.92 /usr/lib/systemd/systemd --switched-root --system --deseriali
360 root 20 0 43860 468 300 S 0.0 0.0 0:00.06 /usr/lib/systemd/systemd-udevd
465 root 16 -4 55416 412 304 S 0.0 0.0 0:00.36 /sbin/auditd -n
453 root 16 -4 55416 412 304 S 0.0 0.0 0:08.89 /sbin/auditd -n
483 dbus 20 0 24404 684 420 S 0.0 0.0 0:01.58 /bin/dbus-daemon --system --address=systemd: --nofork --nopid
645 root 20 0 540M 580 212 S 0.0 0.0 0:00.00 /usr/bin/python -Es /usr/sbin/tuned -l -P
647 root 20 0 540M 580 212 S 0.0 0.0 0:00.00 /usr/bin/python -Es /usr/sbin/tuned -l -P
512 ntp 20 0 44648 716 556 S 0.0 0.0 0:00.39 /usr/sbin/ntpd -u ntp:ntp -g
524 root 20 0 25844 0 0 S 0.0 0.0 0:00.00 /usr/sbin/atd -f
F1Help F2Setup F3SearchF4FilterF5Tree F6SortByF7Nice -F8Nice +F9Kill F10Quit
atop
這個(gè)使用ASCII碼顯示方式的命令行工具是一個(gè)顯示所有進(jìn)程活動(dòng)的性能監(jiān)控工具.它可以展示每日的系統(tǒng)日志以進(jìn)行長(zhǎng)期的進(jìn)程活動(dòng)分析,并高亮顯示過(guò)載的系統(tǒng)使用資源.它包含了CPU,內(nèi)存,交換空間,磁盤(pán)和網(wǎng)絡(luò)層的度量指標(biāo).所有這些功能只需在終端運(yùn)行atop即可
[root@icloud-store ~]# yum install atop
ATOP - icloud-store 2015/11/05 09:32:57 ----------- 4d1h12m12s elapsed
PRC | sys 6m42s | user 10m43s | #proc 84 | #trun 3 | #tslpi 164 | #tslpu 0 | #zombie 0 | #exit 0 |
CPU | sys 0% | user 1% | irq 0% | idle 99% | wait 0% | guest 0% | curf 2.10GHz | curscal ?% |
CPL | avg1 0.00 | avg5 0.01 | avg15 0.05 | | csw 65314627 | intr 29948e3 | | numcpu 1 |
MEM | tot 1.8G | free 73.9M | cache 229.5M | buff 0.1M | slab 49.9M | shmem 91.0M | vmbal 0.0M | hptot 0.0M |
SWP | tot 2.0G | free 724.6M | | | | | vmcom 3.1G | vmlim 2.9G |
PAG | scan 2498022 | steal 2034e3 | stall 1775 | | | | swin 146919 | swout 1166e3 |
DSK | vda | busy 0% | read 56734 | write 180461 | KiB/w 6 | MBr/s 0.01 | MBw/s 0.00 | avio 0.15 ms |
DSK | vdb | busy 0% | read 19736 | write 9950 | KiB/w 468 | MBr/s 0.00 | MBw/s 0.01 | avio 0.23 ms |
DSK | vdc | busy 0% | read 8454 | write 35547 | KiB/w 10 | MBr/s 0.00 | MBw/s 0.00 | avio 0.03 ms |
NET | transport | tcpi 400946 | tcpo 406652 | udpi 1137 | udpo 1160 | tcpao 61 | tcppo 23099 | tcprs 9319 |
NET | network | ipi 537251 | ipo 420832 | ipfrw 0 | deliv 402251 | | icmpi 134 | icmpo 16 |
NET | eth1 ---- | pcki 619245 | pcko 421452 | si 3 Kbps | so 1 Kbps | erri 0 | erro 0 | drpo 0 |
NET | eth0 ---- | pcki 972639 | pcko 2551 | si 1 Kbps | so 0 Kbps | erri 0 | erro 0 | drpo 0 |
NET | lo ---- | pcki 208 | pcko 208 | si 0 Kbps | so 0 Kbps | erri 0 | erro 0 | drpo 0 |
*** system and process activity since boot ***
PID TID RUID EUID THR SYSCPU USRCPU VGROW RGROW RDDSK WRDSK ST EXC S CPUNR CPU CMD 1/4
21109 - elon elon 32 2m28s 4m11s 4.3G 1.0G 1.0G 96700K N- - S 0 0% java
668 - root root 3 18.06s 4m16s 486.4M 16348K 41968K 233.0M N- - S 0 0% fail2ban-serve
97769 - mysql mysql 39 48.77s 46.35s 1.1G 222.9M 122.2M 170.6M N- - S 0 0% mysqld
342 - root root 1 32.04s 23.93s 45528K 11228K 13136K 0K N- - S 0 0% systemd-journa
276 - root root 1 55.31s 0.00s 0K 0K 1248K 0K N- - R 0 0% xfsaild/vda1
491 - root root 5 5.00s 26.01s 540.2M 580K 19116K 8K N- - S 0 0% tuned
501 - root root 3 6.34s 18.01s 311.6M 9044K 6836K 69256K N- - S 0 0% rsyslogd
127329 - root root 1 23.99s 0.00s 0K 0K 0K 0K N- - S 0 0% kworker/0:1
510 - root root 1 6.55s 12.06s 166.3M 1240K 166.2M 64K N- - S 0 0% qemu-ga
iftop
iftop也是類(lèi)似于top的實(shí)時(shí)流量監(jiān)控工具,可以用來(lái)監(jiān)控網(wǎng)卡的實(shí)時(shí)流量(可以指定網(wǎng)段)、反向解析IP、顯示端口信息等.
- 官方網(wǎng)站:http://www.ex-parrot.com/~pdw/iftop/
組件安裝
[root@localhost ~]# yum install flex byacc libpcap ncurses ncurses-devel libpcap-devel
[root@localhost ~]# wget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz
[root@localhost ~]# tar -zxvf iftop-0.17.tar.gz
[root@localhost ~]# cd iftop-0.17
[root@localhost iftop-0.17]# ./configure
[root@localhost iftop-0.17]# make && make install
[root@icloud-store iftop-0.17]# iftop -help
iftop: display bandwidth usage on an interface by host
Synopsis: iftop -h | [-npbBP] [-i interface] [-f filter code] [-N net/mask]
-h display this message
-n don't do hostname lookups
-N don't convert port numbers to services
-p run in promiscuous mode (show traffic between other
hosts on the same network segment)
-b don't display a bar graph of traffic
-B Display bandwidth in bytes
-i interface listen on named interface
-f filter code use filter code to select packets to count
(default: none, but only IP packets are counted)
-F net/mask show traffic flows in/out of network
-P show ports as well as hosts
-m limit sets the upper limit for the bandwidth scale
-c config file specifies an alternative configuration file
iftop, version 0.17
copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors
應(yīng)用文檔
- https://blog.fazero.me/2015/07/12/Linux%E6%B5%81%E9%87%8F%E7%9B%91%E6%8E%A7%E5%B7%A5%E5%85%B7-iftop/
Linux命令行工具 - iostat
iostat主要用于監(jiān)控系統(tǒng)設(shè)備的IO負(fù)載情況,iostat首次運(yùn)行時(shí)顯示自系統(tǒng)啟動(dòng)開(kāi)始的各項(xiàng)統(tǒng)計(jì)信息,之后運(yùn)行iostat將顯示自上次運(yùn)行該命令以后的統(tǒng)計(jì)信息.用戶(hù)可以通過(guò)指定統(tǒng)計(jì)的次數(shù)和時(shí)間來(lái)獲得所需的統(tǒng)計(jì)信息.
命令語(yǔ)法
[root@icloud-store ~]# iostat --help
用法: iostat [ 選項(xiàng) ] [ <時(shí)間間隔> [ <次數(shù)> ] ]
選項(xiàng):
[ -c ] [ -d ] [ -h ] [ -k | -m ] [ -N ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]
[ -j { ID | LABEL | PATH | UUID | ... } ]
[ [ -T ] -g <用戶(hù)組名> ] [ -p [ <設(shè)備> [,...] | ALL ] ]
[ <設(shè)備> [...] | ALL ]
應(yīng)用示例
查看TPS和吞吐量信息
每隔2秒顯示一次設(shè)備(磁盤(pán))使用狀態(tài),-k標(biāo)識(shí)使用kb為單位
[root@icloud-store ~]# iostat -d -k 2
Linux 3.10.0-514.21.1.el7.x86_64 (icloud-store) 2017年11月05日 _x86_64_ (1 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 0.70 11.55 3.53 4167729 1275898
vdb 0.08 1.64 12.92 590641 4662340
vdc 0.13 0.51 1.10 184877 395668
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 0.00 0.00 0.00 0 0
vdb 0.00 0.00 0.00 0 0
vdc 0.00 0.00 0.00 0 0
- Device: 檢測(cè)設(shè)備名稱(chēng)
- tps:該設(shè)備每秒的傳輸次數(shù)("一次傳輸"意思是"一次I/O請(qǐng)求"),多個(gè)邏輯請(qǐng)求可能會(huì)被合并為"一次I/O請(qǐng)求","一次傳輸"請(qǐng)求的大小是未知的.
- kB_read/s:每秒從設(shè)備(drive expressed)讀取的數(shù)據(jù)量;
- kB_wrtn/s:每秒向設(shè)備(drive expressed)寫(xiě)入的數(shù)據(jù)量;
- kB_read:讀取的總數(shù)據(jù)量;
- kB_wrtn:寫(xiě)入的總數(shù)量數(shù)據(jù)量;
顯示所有設(shè)備負(fù)載情況
[root@icloud-store ~]# iostat
Linux 3.10.0-514.21.1.el7.x86_64 (icloud-store) 2017年11月05日 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.70 0.00 0.41 0.00 0.00 98.88
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 0.70 11.55 3.53 4167729 1275817
vdb 0.08 1.64 12.92 590641 4662340
vdc 0.13 0.51 1.10 184877 395648
只查看cpu狀態(tài)
[root@icloud-store ~]# iostat -c 1 2
Linux 3.10.0-514.21.1.el7.x86_64 (icloud-store) 2017年11月05日 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.70 0.00 0.41 0.00 0.00 98.88
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00
- %user:CPU處在用戶(hù)模式下的時(shí)間百分比.
- %nice:CPU處在帶NICE值的用戶(hù)模式下的時(shí)間百分比.
- %system:CPU處在系統(tǒng)模式下的時(shí)間百分比.
- %iowait:CPU等待輸入輸出完成時(shí)間的百分比.
- %steal:管理程序維護(hù)另一個(gè)虛擬處理器時(shí),虛擬CPU的無(wú)意識(shí)等待時(shí)間百分比.
- %idle:CPU空閑時(shí)間百分比.
查看更多擴(kuò)展信息
iostat還有一個(gè)比較常用的選項(xiàng)-x,該選項(xiàng)將用于顯示和io相關(guān)的擴(kuò)展數(shù)據(jù).
[root@icloud-store ~]# iostat -d -k -x 1 10
Linux 3.10.0-514.21.1.el7.x86_64 (icloud-store) 2017年11月05日 _x86_64_ (1 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 0.02 0.18 0.52 11.54 3.53 43.28 0.00 1.02 0.99 1.03 0.15 0.01
vdb 0.35 3.20 0.05 0.03 1.64 12.91 353.64 0.00 1.91 0.20 5.31 0.23 0.00
vdc 0.00 0.10 0.02 0.10 0.51 1.10 25.71 0.00 0.09 0.35 0.04 0.03 0.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
vdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
vdc 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 1.01 0.00 4.04 0.00 24.24 12.00 0.00 1.00 0.00 1.00 0.25 0.10
vdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
vdc 0.00 1.01 0.00 4.04 0.00 20.20 10.00 0.00 0.00 0.00 0.00 0.00 0.00
- rrqm/s: 每秒進(jìn)行merge的讀操作數(shù)目.即 rmerge/s
- wrqm/s: 每秒進(jìn)行merge的寫(xiě)操作數(shù)目.即 wmerge/s
- r/s: 每秒完成的讀 I/O 設(shè)備次數(shù).即 rio/s
- w/s: 每秒完成的寫(xiě) I/O 設(shè)備次數(shù).即 wio/s
- rsec/s: 每秒讀扇區(qū)數(shù).即 rsect/s
- wsec/s: 每秒寫(xiě)扇區(qū)數(shù).即 wsect/s
- rkB/s: 每秒讀K字節(jié)數(shù).是 rsect/s 的一半,因?yàn)槊可葏^(qū)大小為512字節(jié).
- wkB/s: 每秒寫(xiě)K字節(jié)數(shù).是 wsect/s 的一半.
- avgrq-sz: 平均每次設(shè)備I/O操作的數(shù)據(jù)大小(扇區(qū))
- avgqu-sz: 平均I/O隊(duì)列長(zhǎng)度.
- await: 平均每次設(shè)備I/O操作的等待時(shí)間(毫秒),一般地系統(tǒng)IO響應(yīng)時(shí)間應(yīng)該低于5ms,如果大于10ms就比較大了.
- svctm: 平均每次設(shè)備I/O操作的服務(wù)時(shí)間(毫秒).
- %util: 一秒中有百分之多少的時(shí)間用于 I/O 操作,即被io消耗的cpu百分比,參數(shù)暗示了設(shè)備的繁忙程度,一般地,如果該參數(shù)是100%表示設(shè)備已經(jīng)接近滿(mǎn)負(fù)荷運(yùn)行了(當(dāng)然如果是多磁盤(pán),即使%util是100%,因?yàn)榇疟P(pán)的并發(fā)能力,所以磁盤(pán)使用未必就到了瓶頸).
備注: 如果 %util 接近 100%,說(shuō)明產(chǎn)生的I/O請(qǐng)求太多,I/O系統(tǒng)已經(jīng)滿(mǎn)負(fù)荷,該磁盤(pán)可能存在瓶頸.如果 svctm 比較接近 await,說(shuō)明 I/O 幾乎沒(méi)有等待時(shí)間;如果 await 遠(yuǎn)大于 svctm,說(shuō)明I/O 隊(duì)列太長(zhǎng),io響應(yīng)太慢,則需要進(jìn)行必要優(yōu)化.如果avgqu-sz比較大,也表示有當(dāng)量io在等待.
Linux命令行工具 - iotop
iotop 是專(zhuān)門(mén)用來(lái)顯示硬盤(pán) IO 的命令,對(duì)每個(gè)進(jìn)程進(jìn)行單獨(dú)的跟蹤和分析.
語(yǔ)法說(shuō)明
[root@icloud-store ~]# iotop --help
Usage: /usr/sbin/iotop [OPTIONS]
DISK READ and DISK WRITE are the block I/O bandwidth used during the sampling
period. SWAPIN and IO are the percentages of time the thread spent respectively
while swapping in and waiting on I/O more generally. PRIO is the I/O priority at
which the thread is running (set using the ionice command).
Controls: left and right arrows to change the sorting column, r to invert the
sorting order, o to toggle the --only option, p to toggle the --processes
option, a to toggle the --accumulated option, i to change I/O priority, q to
quit, any other key to force a refresh.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-o, --only only show processes or threads actually doing I/O
-b, --batch non-interactive mode
-n NUM, --iter=NUM number of iterations before ending [infinite]
-d SEC, --delay=SEC delay between iterations [1 second]
-p PID, --pid=PID processes/threads to monitor [all]
-u USER, --user=USER users to monitor [all]
-P, --processes only show processes, not all threads
-a, --accumulated show accumulated I/O instead of bandwidth
-k, --kilobytes use kilobytes instead of a human friendly unit
-t, --time add a timestamp on each line (implies --batch)
-q, --quiet suppress some lines of header (implies --batch)
參數(shù)說(shuō)明
- -o:只顯示有io操作的進(jìn)程
- -b:批量顯示,無(wú)交互,主要用作記錄到文件.
- -n NUM:顯示NUM次,主要用于非交互式模式.
- -d SEC:間隔SEC秒顯示一次.
- -p PID:監(jiān)控的進(jìn)程pid.
- -u USER:監(jiān)控的進(jìn)程用戶(hù).
- -P, --processes: 僅顯示進(jìn)程,默認(rèn)iotop顯示所有線(xiàn)程
- -a, --accumulated: 顯示累積的I/O,而不是帶寬
- -k, --kilobytes: 使用kB單位,而不是對(duì)人友好的單位.在非交互模式下,腳本編程有用.
- -t, --time: 加上時(shí)間戳,非交互非模式.
- -q, --quiet: 禁止頭幾行,非交互模式; 有三種指定方式(-q:只在第一次監(jiān)測(cè)時(shí)顯示列名、-qq:永遠(yuǎn)不顯示列名、-qqq:永遠(yuǎn)不顯示I/O匯總)
應(yīng)用示例
[root@icloud-store ~]# yum install iotop
[root@icloud-store ~]# iotop
Total DISK READ: 0.00 B/s | Total DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
26186 be/4 elon 0.00 B/s 3.90 K/s 0.00 % 0.00 % ./bin/log_agent_file --log_conf=~-conf=./conf/log_agent_file.conf
1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % init
2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd]
3 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/0]
4 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/0]
5 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/0]
6 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/0]
7 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/1]
8 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/1]
9 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/1]
10 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/1]
11 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/2]
12 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/2]
13 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/2]
14 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/2]
15 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/3]
16 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/3]
17 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/3]
18 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/3]
19 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [events/0]
20 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [events/1]
21 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [events/2]
22 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [events/3]
23 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [cgroup]
24 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [khelper]
25 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [netns]
通過(guò)輸出結(jié)果,我們可以清楚地知曉是什么程序在讀寫(xiě)磁盤(pán),速度以及命令行,pid 等信息.
常用快捷鍵
- 左右箭頭:改變排序方式,默認(rèn)是按IO排序.
- r:改變排序順序.
- o:只顯示有IO輸出的進(jìn)程.
- p:進(jìn)程/線(xiàn)程的顯示方式的切換.
- a:顯示累積使用量.
- q:退出.
Linux命令行工具 - uptime
uptime命令主要用于獲取主機(jī)運(yùn)行時(shí)間和查詢(xún)linux系統(tǒng)負(fù)載等信息
命令語(yǔ)法
[root@icloud-store ~]# uptime -h
Usage:
uptime [options]
Options:
-p, --pretty show uptime in pretty format
-h, --help display this help and exit
-s, --since system up since
-V, --version output version information and exit
For more details see uptime(1).
應(yīng)用示例
[root@icloud-store ~]# uptime
10:27:00 up 4 days, 3:36, 2 users, load average: 0.00, 0.02, 0.05
顯示依次為:
現(xiàn)在時(shí)間、系統(tǒng)累計(jì)運(yùn)行時(shí)長(zhǎng)、當(dāng)前登陸用戶(hù)數(shù)、系統(tǒng)在過(guò)去的1分鐘、5分鐘和15分鐘內(nèi)的平均負(fù)載
如何查看
通常我們先看15分鐘load,如果load很高,再看1分鐘和5分鐘負(fù)載,查看是否有下降趨勢(shì); 1分鐘負(fù)載值>1,不用擔(dān)心,但是如果15分鐘負(fù)載都超過(guò)1,我們要趕緊看看發(fā)生了什么事情;所以我們要根據(jù)實(shí)際情況查看這三個(gè)值.
查看cpu信息
[root@icloud-store ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 6
model name : QEMU Virtual CPU version 2.5+
stepping : 3
microcode : 0x1
cpu MHz : 2099.996
cache size : 4096 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pse36 clflush mmx fxsr sse sse2 ss syscall nx lm rep_good nopl pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 aes xsave avx hypervisor lahf_lm avx2
bogomips : 4199.99
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management:
查看cpu核數(shù)
[root@icloud-store ~]# grep 'model name' /proc/cpuinfo | wc -l
1
類(lèi)似命令
w和top命令也能看查看平均負(fù)載
[root@icloud-store ~]# w
10:34:06 up 4 days, 3:43, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/2 123.116.48.212 07:42 2:05m 0.08s 0.08s -bash
root pts/3 123.116.48.212 09:02 6.00s 0.02s 0.00s w
Linux命令行工具 - vmstat
vmstat命令是最常見(jiàn)的Linux/Unix監(jiān)控工具,可以展現(xiàn)給定時(shí)間間隔的服務(wù)器的狀態(tài)值,包括服務(wù)器的CPU使用率,內(nèi)存使用,虛擬內(nèi)存交換情況,IO讀寫(xiě)情況
語(yǔ)法說(shuō)明
[root@icloud-store ~]# vmstat -h
Usage:
vmstat [options] [delay [count]]
Options:
-a, --active active/inactive memory
-f, --forks number of forks since boot
-m, --slabs slabinfo
-n, --one-header do not redisplay header
-s, --stats event counter statistics
-d, --disk disk statistics
-D, --disk-sum summarize disk statistics
-p, --partition <dev> partition specific statistics
-S, --unit <char> define display unit
-w, --wide wide output
-t, --timestamp show timestamp
-h, --help display this help and exit
-V, --version output version information and exit
For more details see vmstat(8).
- -a: 顯示活動(dòng)內(nèi)頁(yè);
- -f: 顯示啟動(dòng)后創(chuàng)建的進(jìn)程總數(shù);
- -m: 顯示slab信息;
- -n: 頭信息僅顯示一次;
- -s: 以表格方式顯示事件計(jì)數(shù)器和內(nèi)存狀態(tài);
- -d: 報(bào)告磁盤(pán)狀態(tài);
- -p: 顯示指定的硬盤(pán)分區(qū)狀態(tài);
- -S: 輸出信息的單位
- -w: 寬輸出(格式化展示)
- -t: 輸出時(shí)間戳
示例應(yīng)用
使用vmstat每隔1秒報(bào)告一次服務(wù)器狀態(tài)信息
[root@icloud-store ~]# vmstat 1 -w
procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu--------
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 1327884 73468 1460 248212 2 13 12 18 85 62 1 0 99 0 0
0 0 1327884 75120 1460 248256 0 0 0 84 99 209 1 0 99 0 0
重要參數(shù):
procs(進(jìn)程)
- procs.r : CPU調(diào)度運(yùn)行隊(duì)列的長(zhǎng)度(運(yùn)行隊(duì)列中輕量級(jí)進(jìn)程的實(shí)際數(shù)量).
- procs.b : 在等待io的進(jìn)程數(shù)(表示阻塞的進(jìn)程)
memory(內(nèi)存)
- memory.swpd : 已使用的虛擬內(nèi)存大小;如果大于0表示你的機(jī)器物理內(nèi)存不足了,如果不是程序內(nèi)存泄露的原因,那么你該升級(jí)內(nèi)存了或者把耗內(nèi)存的任務(wù)遷移到其他機(jī)器.
- memory.free : 空閑的物理內(nèi)存打下
- memory.buff : 用作緩沖區(qū)的內(nèi)存數(shù)
- memory.cache : 用作高速緩存的內(nèi)存數(shù)(cache用來(lái)記憶我們打開(kāi)的文件,給文件做緩沖),如果cache的值大的時(shí)候,說(shuō)明cache處的文件數(shù)多,如果頻繁訪(fǎng)問(wèn)到的文件都能被cache處,那么磁盤(pán)的讀IO bi會(huì)非常小.
swap(交換區(qū))
- swap.si : 每秒從交換區(qū)寫(xiě)到內(nèi)存的大小(由磁盤(pán)調(diào)入內(nèi)存)
- swap.so : 每秒寫(xiě)入交換區(qū)的內(nèi)存大小(由內(nèi)存調(diào)入磁盤(pán))
內(nèi)存夠用的時(shí)候,這2個(gè)值都是0,如果這2個(gè)值長(zhǎng)期大于0時(shí),系統(tǒng)性能會(huì)受到影響,磁盤(pán)IO和CPU資源都會(huì)被消耗.有些朋友看到空閑內(nèi)存(free)很少的或接近于0時(shí),就認(rèn)為內(nèi)存不夠用了,不能光看這一點(diǎn),還要結(jié)合si和so,如果free很少,但是si和so也很少(大多時(shí)候是0),那么不用擔(dān)心,系統(tǒng)性能這時(shí)不會(huì)受到影響的.
io
- io.bi : 每秒讀取的塊數(shù)(塊/秒)
- io.bo : 每秒寫(xiě)入的塊數(shù)(塊/秒)
system
- system.in : 每秒的中斷數(shù),包括時(shí)鐘中斷
- system.cs : 每秒的cpu交換(上下文切換)的次數(shù)(進(jìn)程切換的過(guò)程被稱(chēng)作上下文切換)
cpu
- cpu.us : 用戶(hù)進(jìn)程CPU使用率(%)
- cpu.sy : 系統(tǒng)進(jìn)程CPU使用率(%)
- cpu.id : 空閑率或者CPU可用率(%)
- cpu.wa : 等待IO所消耗的CPU的時(shí)間(%)
- cpu.st : 從虛擬設(shè)備中獲得的時(shí)間(%)
Linux命令行工具 - scp
scp(secure copy)用來(lái)進(jìn)行遠(yuǎn)程拷貝文件的命令;有時(shí)我們需要獲得遠(yuǎn)程服務(wù)器上的某個(gè)文件,該服務(wù)器既沒(méi)有配置ftp服務(wù)器,也沒(méi)有做共享,無(wú)法通過(guò)常規(guī)途徑獲得文件時(shí),只需要通過(guò)簡(jiǎn)單的scp命令便可達(dá)到目的.
使用示例
從本地機(jī)器上傳文件(夾)到服務(wù)器
[root@localhost ~]# scp -r /path/on/local user@ip:/path/on/server
從遠(yuǎn)程服務(wù)器下載文件(夾)到本機(jī)
[root@localhost ~]# scp -r user@ip:/path/on/server /path/on/local
說(shuō)明
- r標(biāo)識(shí)文件夾傳遞是可以進(jìn)行文件夾遍歷
- 使用scp要注意所使用的用戶(hù)是否具有可讀取遠(yuǎn)程服務(wù)器相應(yīng)文件的權(quán)限.
- 如果遠(yuǎn)程服務(wù)器防火墻有特殊限制,scp便要走特殊端口,具體用什么端口視情況而定,命令格式如下:
[root@localhost ~]# scp -p 4588 user@ip:/path/on/server /path/on/local
Linux命令行工具 - crontab
crontab命令的功能是在一定的時(shí)間間隔調(diào)度一些命令的執(zhí)行.在/etc目錄下有一個(gè)crontab文件,這里存放有系統(tǒng)運(yùn)行的一些調(diào)度程序.每個(gè)用戶(hù)可以建立自己的調(diào)度crontab.
命令語(yǔ)法
[root@localhost ~]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
可以看出,前面分別是分鐘,小時(shí),天,月和周,最后是shell命令.cron調(diào)度的最小頻率為1分鐘
| 意義 | 分鐘 | 小時(shí) | 天 | 月份 | 周 | 命令 |
|---|---|---|---|---|---|---|
| 范圍 | 0-59 | 0-23 | 1-31 | 1-12 | 0-7 | shell命令 |
說(shuō)明:
- "周"這一欄中0和7都代表周日
- 每個(gè)月的最后一日無(wú)法由crontab直接支持,需要通過(guò)腳本判斷
- 命令最好是絕對(duì)路徑
還有一些輔助的符號(hào),可以借助他們寫(xiě)出更靈活的調(diào)度
| 特殊字符 | 代表意義 | 示例 |
|---|---|---|
| * | 代表任何時(shí)候都接受 | * * * * * command |
| , | 并列時(shí)間 | * 3,6,9,12 * * * command |
| - | 連續(xù)區(qū)間 | * 9-17 * * * command |
| /N | N表示每隔N個(gè)單位之間 | */5 * * * * command |
crontab主要有4個(gè)參數(shù):
- -l #列出某個(gè)用戶(hù)的cron服務(wù)的詳細(xì)內(nèi)容
- -u #設(shè)定某個(gè)用戶(hù)的cron服務(wù),一般是root用戶(hù)使用此選項(xiàng)
- -r #刪除某個(gè)用戶(hù)的cron服務(wù)
- -e #編輯某個(gè)用戶(hù)的cron服務(wù)
應(yīng)用示例
當(dāng)前用戶(hù)添加新的定時(shí)任務(wù)
[root@localhost ~ ]$ crontab -e
*/1 * * * * echo 'test crontab' >> /opt/py/cron.txt
查看當(dāng)前用戶(hù)的系統(tǒng)定時(shí)任務(wù)
[root@localhost ~ ]$ crontab -l
*/1 * * * * echo 'test crontab' >> /opt/py/cron.txt
刪除crontab文件
[root@localhost ~ ]$ crontab -r
喜歡我就關(guān)注我吧!! 微信公眾號(hào):異次猿
