調(diào)試排錯(cuò) - Java 問(wèn)題排查之Linux命令
本文是JVM第十七講, Java 問(wèn)題調(diào)試排錯(cuò)。Java 在線問(wèn)題排查主要分兩篇:本文是第一篇,通過(guò)linux常用命令排查。
在項(xiàng)目中,日志操作的常用命令
1、在終端中登錄日志系統(tǒng)
- 賬號(hào): ssh developer@172.16.101.123 密碼: zcy 日志中心
2、查看模塊位置的指令
- find -name web-item* 或 find ./ -name item-mi* (模糊搜索以web-item開(kāi)頭的模塊)
3、查看錯(cuò)誤日志
- 方式1: tail -f filename(filename為前一個(gè)命令查到的路徑)
- 方式2:通過(guò) vi filename 來(lái)查詢 可以更加方便地搜索日志中的關(guān)鍵字
注意事項(xiàng)
- 日志操作命令僅能查看線下環(huán)境(dev-debug/test),staging和真線必須使用“日志平臺(tái)”進(jìn)行查看日志平臺(tái)地址
常用vi命令
- shift+g 查看最新日志 ?關(guān)鍵字 結(jié)合 n /n 關(guān)鍵字搜索
具體可以參考這篇文章:開(kāi)發(fā)工具篇第九講:菜鳥(niǎo)入坑指南
1、文本操作
1.1、文本查找 - grep (常用)
grep常用命令:
# 基本使用
grep yoursearchkeyword file.txt #文件查找
grep 'KeyWord otherKeyWord' file.txt cpf.txt #多文件查找, 含空格加引號(hào)
grep 'KeyWord' /home/admin -r -n #目錄下查找所有符合關(guān)鍵字的文件
grep 'keyword' /home/admin -r -n -i # -i 忽略大小寫(xiě)
grep 'KeyWord' /home/admin -r -n --include *.{vm,java} #指定文件后綴
grep 'KeyWord' /home/admin -r -n --exclude *.{vm,java} #反匹配
# cat + grep
cat file.txt | grep -i keyword # 查找所有keyword且不分大小寫(xiě)
cat file.txt | grep -c 'KeyWord' # 統(tǒng)計(jì)Keyword次數(shù)
# seq + grep
seq 10 | grep 5 -A 3 #上匹配
seq 10 | grep 5 -B 3 #下匹配
seq 10 | grep 5 -C 3 #上下匹配,平時(shí)用這個(gè)就妥了
Grep的參數(shù):
--color=auto:顯示顏色;
-i, --ignore-case:忽略字符大小寫(xiě);
-o, --only-matching:只顯示匹配到的部分;
-n, --line-number:顯示行號(hào);
-v, --invert-match:反向顯示,顯示未匹配到的行;
-E, --extended-regexp:支持使用擴(kuò)展的正則表達(dá)式;
-q, --quiet, --silent:靜默模式,即不輸出任何信息;
-w, --word-regexp:整行匹配整個(gè)單詞;
-c, --count:統(tǒng)計(jì)匹配到的行數(shù); print a count of matching lines;
-B, --before-context=NUM:print NUM lines of leading context 后#行
-A, --after-context=NUM:print NUM lines of trailing context 前#行
-C, --context=NUM:print NUM lines of output context 前后各#行
1.2、文本分析 - awk (不常用)
awk基本命令:
# 基本使用
awk '{print $4,$6}' file.txt
awk '{print NR,$0}' file.txt cpf.txt
awk '{print FNR,$0}' file.txt cpf.txt
awk '{print FNR,FILENAME,$0}' file.txt cpf.txt
awk '{print FILENAME,"NR="NR,"FNR="FNR,"$"NF"="$NF}' file.txt cpf.txt
echo 1:2:3:4 | awk -F: '{print $1,$2,$3,$4}'
# 匹配
awk '/ldb/ {print}' file.txt #匹配ldb
awk '!/ldb/ {print}' file.txt #不匹配ldb
awk '/ldb/ && /LISTEN/ {print}' file.txt #匹配ldb和LISTEN
awk '$5 ~ /ldb/ {print}' file.txt #第五列匹配ldb
內(nèi)建變量
`NR`: NR表示從awk開(kāi)始執(zhí)行后,按照記錄分隔符讀取的數(shù)據(jù)次數(shù),默認(rèn)的記錄分隔符為換行符,因此默認(rèn)的就是讀取的數(shù)據(jù)行數(shù),NR可以理解為Number of Record的縮寫(xiě)。
`FNR`: 在awk處理多個(gè)輸入文件的時(shí)候,在處理完第一個(gè)文件后,NR并不會(huì)從1開(kāi)始,而是繼續(xù)累加,因此就出現(xiàn)了FNR,每當(dāng)處理一個(gè)新文件的時(shí)候,F(xiàn)NR就從1開(kāi)始計(jì)數(shù),F(xiàn)NR可以理解為File Number of Record。
`NF`: NF表示目前的記錄被分割的字段的數(shù)目,NF可以理解為Number of Field。
更多請(qǐng)參考:Linux awk 命令
1.3、文本處理 - sed (不常用)
sed常用:
# 文本打印
sed -n '3p' file.log #只打印第三行
sed -n '$p' file.log #只打印最后一行
sed -n '3,9p' file.log #只查看文件的第3行到第9行
sed -n -e '3,9p' -e '=' file.log #打印3-9行,并顯示行號(hào)
sed -n '/root/p' file.log #顯示包含root的行
sed -n '/hhh/,/omc/p' file.log # 顯示包含"hhh"的行到包含"omc"的行之間的行
# 文本替換
sed -i 's/root/world/g' file.log # 用 world 替換file.log文件中的root; s==search 查找并替換, g==global 全部替換, -i: implace
# 文本插入
sed '1,4i hahaha' file.log # 在文件第一行和第四行的每行下面添加hahaha
sed -e '1i happy' -e '$a new year' file.log #【界面顯示】在文件第一行添加happy,文件結(jié)尾添加new year
sed -i -e '1i happy' -e '$a new year' file.log #【真實(shí)寫(xiě)入文件】在文件第一行添加happy,文件結(jié)尾添加new year
# 文本刪除
sed '3,9d' file.log # 刪除第3到第9行,只是不顯示而已
sed '/hhh/,/omc/d' file.log # 刪除包含"hhh"的行到包含"omc"的行之間的行
sed '/omc/,10d' file.log # 刪除包含"omc"的行到第十行的內(nèi)容
# 與find結(jié)合
find . -name "*.txt" |xargs sed -i 's/hhhh/\hHHh/g'
find . -name "*.txt" |xargs sed -i 's#hhhh#hHHh#g'
find . -name "*.txt" -exec sed -i 's/hhhh/\hHHh/g' {} \;
find . -name "*.txt" |xargs cat
更多請(qǐng)參考:Linux sed 命令 或者 Linux sed命令詳解
2、文件操作
2.1、文件監(jiān)聽(tīng) - tail (常用)
最常用的 tail -f filename
# 基本使用
tail -f file.log # 循環(huán)監(jiān)聽(tīng)文件
tail -300f file.log # 倒數(shù)300行并追蹤文件
tail +20 file.log # 從第20行至文件末尾顯示文件內(nèi)容
# tail f使用
tail f file.log #等同于tail -f -n 10 打印最后10行,然后追蹤文件
tail -f 與tail F 與tailf三者區(qū)別
`tail -f` 等于--follow=descriptor,根據(jù)文件描述進(jìn)行追蹤,當(dāng)文件改名或刪除后,停止追蹤。
`tail -F` 等于 --follow=name ==retry,根據(jù)文件名字進(jìn)行追蹤,當(dāng)文件改名或刪除后,保持重試,當(dāng)有新的文件和他同名時(shí),繼續(xù)追蹤
`tail f` 等于tail -f -n 10(tail -f或-F默認(rèn)也是打印最后10行,然后追蹤文件),與tail -f不同的是,如果文件不增長(zhǎng),它不會(huì)去訪問(wèn)磁盤文件,所以 tailf 特別適合那些便攜機(jī)上跟蹤日志文件,因?yàn)樗鼫p少了磁盤訪問(wèn),可以省電。
tail的參數(shù)
-f 循環(huán)讀取
-q 不顯示處理信息
-v 顯示詳細(xì)的處理信息
-c<數(shù)目> 顯示的字節(jié)數(shù)
-n<行數(shù)> 顯示文件的尾部 n 行內(nèi)容
--pid=PID 與-f合用,表示在進(jìn)程ID,PID死掉之后結(jié)束
-q, --quiet, --silent 從不輸出給出文件名的首部
-s, --sleep-interval=S 與-f合用,表示在每次反復(fù)的間隔休眠S秒
2.2、文件查找 - find (常用)
sudo -u admin find /home/admin /tmp /usr -name \*.log(多個(gè)目錄去找)
find . -iname \*.txt(大小寫(xiě)都匹配)
find . -type d(當(dāng)前目錄下的所有子目錄)
find /usr -type l(當(dāng)前目錄下所有的符號(hào)鏈接)
find /usr -type l -name "z*" -ls(符號(hào)鏈接的詳細(xì)信息 eg:inode,目錄)
find /home/admin -size +250000k(超過(guò)250000k的文件,當(dāng)然+改成-就是小于了)
find /home/admin f -perm 777 -exec ls -l {} \; (按照權(quán)限查詢文件)
find /home/admin -atime -1 1天內(nèi)訪問(wèn)過(guò)的文件
find /home/admin -ctime -1 1天內(nèi)狀態(tài)改變過(guò)的文件
find /home/admin -mtime -1 1天內(nèi)修改過(guò)的文件
find /home/admin -amin -1 1分鐘內(nèi)訪問(wèn)過(guò)的文件
find /home/admin -cmin -1 1分鐘內(nèi)狀態(tài)改變過(guò)的文件
find /home/admin -mmin -1 1分鐘內(nèi)修改過(guò)的文件
2.3、pgm (不常用)
批量查詢 vm-shopbase 滿足條件的日志
pgm -A -f vm-shopbase 'cat /home/admin/shopbase/logs/shopbase.log.2017-01-17|grep 2069861630'
3、查看網(wǎng)絡(luò)和進(jìn)程
3.1、查看所有網(wǎng)絡(luò)接口的屬性 (常用)
[root@qiwenjie ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.165.194 netmask 255.255.240.0 broadcast 172.31.175.255
ether 00:16:3e:08:c1:ea txqueuelen 1000 (Ethernet)
RX packets 21213152 bytes 2812084823 (2.6 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 25264438 bytes 46566724676 (43.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 502 bytes 86350 (84.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 502 bytes 86350 (84.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.2、查看防火墻設(shè)置 (不常用)
[root@qiwenjie ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
3.3、查看路由表 (不常用)
[root@qiwenjie ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.31.175.253 0.0.0.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
172.31.160.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
3.4、netstat (不常用)
查看所有監(jiān)聽(tīng)端口
[root@qiwenjie ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 970/nginx: master p
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 1249/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 970/nginx: master p
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1547/sshd
tcp6 0 0 :::3306 :::* LISTEN 1894/mysqld
查看所有已經(jīng)建立的連接
[root@qiwenjie ~]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 970/nginx: master p
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 1249/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 970/nginx: master p
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1547/sshd
tcp 0 0 172.31.165.194:53874 100.100.30.25:80 ESTABLISHED 18041/AliYunDun
tcp 0 64 172.31.165.194:22 xxx.194.1.200:2649 ESTABLISHED 32516/sshd: root@pt
tcp6 0 0 :::3306 :::* LISTEN 1894/m
查看當(dāng)前連接
[root@qiwenjie ~]# netstat -nat|awk '{print $6}'|sort|uniq -c|sort -rn
5 LISTEN
2 ESTABLISHED
1 Foreign
1 established)
查看網(wǎng)絡(luò)統(tǒng)計(jì)信息進(jìn)程
[root@qiwenjie ~]# netstat -s
Ip:
21017132 total packets received
0 forwarded
0 incoming packets discarded
21017131 incoming packets delivered
25114367 requests sent out
324 dropped because of missing route
Icmp:
18088 ICMP messages received
692 input ICMP message failed.
ICMP input histogram:
destination unreachable: 4241
timeout in transit: 19
echo requests: 13791
echo replies: 4
timestamp request: 33
13825 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 1
echo replies: 13791
timestamp replies: 33
IcmpMsg:
InType0: 4
InType3: 4241
InType8: 13791
InType11: 19
InType13: 33
OutType0: 13791
OutType3: 1
OutType14: 33
Tcp:
12210 active connections openings
208820 passive connection openings
54198 failed connection attempts
9805 connection resets received
...
netstat 請(qǐng)參考這篇文章: Linux netstat命令詳解
3.5、查看所有進(jìn)程 (常用)
[root@qiwenjie ~]# ps -ef | grep java
root 1249 1 0 Nov04 ? 00:58:05 java -jar /opt/tech_doc/bin/tech_arch-0.0.1-RELEASE.jar --server.port=9999
root 32718 32518 0 08:36 pts/0 00:00:00 grep --color=auto java
3.6、top (常用)
top除了看一些基本信息之外,剩下的就是配合來(lái)查詢vm的各種問(wèn)題了
# top -H -p pid
top - 08:37:51 up 45 days, 18:45, 1 user, load average: 0.01, 0.03, 0.05
Threads: 28 total, 0 running, 28 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.7 us, 0.7 sy, 0.0 ni, 98.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1882088 total, 74608 free, 202228 used, 1605252 buff/cache
KiB Swap: 2097148 total, 1835392 free, 261756 used. 1502036 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1347 root 20 0 2553808 113752 1024 S 0.3 6.0 48:46.74 VM Periodic Tas
1249 root 20 0 2553808 113752 1024 S 0.0 6.0 0:00.00 java
1289 root 20 0 2553808 113752 1024 S 0.0 6.0 0:03.74 java
...
4、查看磁盤和內(nèi)存相關(guān)
4.1、查看內(nèi)存使用 - free -m (常用)
[root@qiwenjie ~]# free -m
total used free shared buff/cache available
Mem: 1837 196 824 0 816 1469
Swap: 2047 255 1792
4.2、查看各分區(qū)使用情況 (常用)
[root@qiwenjie ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 909M 0 909M 0% /dev
tmpfs 919M 0 919M 0% /dev/shm
tmpfs 919M 452K 919M 1% /run
tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/vda1 40G 15G 23G 40% /
tmpfs 184M 0 184M 0% /run/user/0
4.3、查看指定目錄的大小 (常用)
[root@qiwenjie ~]# du -sh
803M
4.4、查看內(nèi)存總量 (常用)
[root@qiwenjie ~]# grep MemTotal /proc/meminfo
MemTotal: 1882088 kB
4.5、查看空閑內(nèi)存量 (常用)
[root@qiwenjie ~]# grep MemFree /proc/meminfo
MemFree: 74120 kB
4.6、查看系統(tǒng)負(fù)載磁盤和分區(qū) (常用)
[root@qiwenjie ~]# grep MemFree /proc/meminfo
MemFree: 74120 kB
4.7、查看系統(tǒng)負(fù)載磁盤和分區(qū) (常用)
[root@qiwenjie ~]# cat /proc/loadavg
0.01 0.04 0.05 2/174 32751
4.8、查看掛接的分區(qū)狀態(tài) (常用)
[root@qiwenjie ~]# mount | column -t
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=930732k,nr_inodes=232683,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
...
4.9、查看所有分區(qū) (常用)
[root@qiwenjie ~]# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008d73a
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83884031 41940992 83 Linux
4.10、查看所有交換分區(qū)
[root@qiwenjie ~]# swapon -s
Filename Type Size Used Priority
/etc/swap file 2097148 261756 -2
4.11、查看硬盤大小
[root@qiwenjie ~]# fdisk -l |grep Disk
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Disk label type: dos
Disk identifier: 0x0008d73a
5、查看用戶和組相關(guān)
5.1、查看活動(dòng)用戶
[root@qiwenjie ~]# w
08:47:20 up 45 days, 18:54, 1 user, load average: 0.01, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 xxx.194.1.200 08:32 0.00s 0.32s 0.32s -bash
5.2、查看指定用戶信息
[root@qiwenjie ~]# id
uid=0(root) gid=0(root) groups=0(root)
5.3、查看用戶登錄日志
[root@qiwenjie ~]# last
root pts/0 xxx.194.1.200 Fri Dec 20 08:32 still logged in
root pts/0 xxx.73.164.60 Thu Dec 19 21:47 - 00:28 (02:41)
root pts/0 xxx.106.236.255 Thu Dec 19 16:00 - 18:24 (02:23)
root pts/1 xxx.194.3.173 Tue Dec 17 13:35 - 17:37 (04:01)
root pts/0 xxx.194.3.173 Tue Dec 17 13:35 - 17:37 (04:02)
...
5.4、查看系統(tǒng)所有用戶
[root@qiwenjie ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
...
5.5、查看系統(tǒng)所有組
cut -d: -f1 /etc/group
6、查看服務(wù),模塊和包相關(guān)
# 查看當(dāng)前用戶的計(jì)劃任務(wù)服務(wù)
crontab -l
# 列出所有系統(tǒng)服務(wù)
chkconfig –list
# 列出所有啟動(dòng)的系統(tǒng)服務(wù)程序
chkconfig –list | grep on
# 查看所有安裝的軟件包
rpm -qa
# 列出加載的內(nèi)核模塊
lsmod
7、查看系統(tǒng),設(shè)備,環(huán)境信息
# 常用
env # 查看環(huán)境變量資源
uptime # 查看系統(tǒng)運(yùn)行時(shí)間、用戶數(shù)、負(fù)載
lsusb -tv # 列出所有USB設(shè)備的linux系統(tǒng)信息命令
lspci -tv # 列出所有PCI設(shè)備
head -n 1 /etc/issue # 查看操作系統(tǒng)版本,是數(shù)字1不是字母L
uname -a # 查看內(nèi)核/操作系統(tǒng)/CPU信息的linux系統(tǒng)信息命令
# /proc/
cat /proc/cpuinfo :查看CPU相關(guān)參數(shù)的linux系統(tǒng)命令
cat /proc/partitions :查看linux硬盤和分區(qū)信息的系統(tǒng)信息命令
cat /proc/meminfo :查看linux系統(tǒng)內(nèi)存信息的linux系統(tǒng)命令
cat /proc/version :查看版本,類似uname -r
cat /proc/ioports :查看設(shè)備io端口
cat /proc/interrupts :查看中斷
cat /proc/pci :查看pci設(shè)備的信息
cat /proc/swaps :查看所有swap分區(qū)的信息
cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep "physical id"
8、tsar
tsar是淘寶開(kāi)源的的采集工具。很好用, 將歷史收集到的數(shù)據(jù)持久化在磁盤上,所以我們快速來(lái)查詢歷史的系統(tǒng)數(shù)據(jù)。當(dāng)然實(shí)時(shí)的應(yīng)用情況也是可以查詢的啦。大部分機(jī)器上都有安裝。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-721908.html
tsar ##可以查看最近一天的各項(xiàng)指標(biāo)
tsar --live ##可以查看實(shí)時(shí)指標(biāo),默認(rèn)五秒一刷
tsar -d 20161218 ##指定查看某天的數(shù)據(jù),貌似最多只能看四個(gè)月的數(shù)據(jù)
tsar --mem
tsar --load
tsar --cpu ##當(dāng)然這個(gè)也可以和-d參數(shù)配合來(lái)查詢某天的單個(gè)指標(biāo)的情況
具體可以看這篇文章:linux 淘寶開(kāi)源監(jiān)控工具tsar文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-721908.html
到了這里,關(guān)于JVM第十七講:調(diào)試排錯(cuò) - Java 問(wèn)題排查之Linux命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!