Linux调试命令
资源统计
# sysstat工具包
yum install sysstat
mpstat -P ALL 1
top -p <pid
## 内存,IO相关
free -m
vmstat 1
iostat -xz 1
pidstat
# 间隔1秒
pidstat 1
# 分别统计CPU、内存、IO,12345表示指定pid
pidstat -u -p 12345
pidstat -r 1
pidstat -d 1
# 找出高内存占用进程
pidstat -l -r|sort -k8nr
# 上下文切换
pid -w -p 12345
显示 PPID
ps -efj
ps -f 2072
pstree -A
ps xao pid,ppid,pgid,sid,comm | head
abrt-cli
查询系统进程异常退出
abrt-cli list
# 某时间之后
abrt-cli list --since 1621904468
# 辅助命令
# 当前时间戳
date +%s
# 时间戳转时间
date -d @1621904468
uptime
dmesg 内核日志
dmesg | tail
文件/进程类
ps
# 查看用户线程数
ps -fLu xulz
# 根据pid找到process
ps -p <pid -o comm=
# 显示不截断内容ww
ps auxww
ps aux | less -+S
ps aux | most -w
# 等价于
ps -ef
# 运行队列
ps -eo stat,pid,user,command | egrep "^STAT|^D|^R"
# D : Uninterruptible sleep (usually IO)
# R : Running or runnable (on run queue)
进程检索/管理
# 显示进程树
pstree -p
pgrep
# 获得java进程的pid
pidof java
kill/pkill用法
# 默认用SIGTERM (terminate)信号终止进程
kill <porcess_id
# 列出可用(不带SIG前缀的)信号名
kill -l
# 立即终止进程
kill -9|KILL <process_id
# 根据进程名称(而不是id)
pkill <process_name
pkill -f "command_name" # 匹配完整 command line
killall java # 等价于 kill `pidof java`
pkill firefox # 基于进程名,等价于 kill `ps --no-headers -C firefox -o pid`
# 发送SIGTERM的4种方式
kill 1234
kill -s TERM 1234
kill -TERM 1234
kill -15 1234
# 发送SIGKILL的3种方式
kill -s KILL 1234
kill -KILL 1234
kill -9 1234
lsof
列出文件打开情况
# 查看某进程打开连接
lsof -i -a -p `pidof firefox`
# 或者
ss -nap | grep $(pidof firefox)
# 查看端口占用 lsof -i :portNumber
lsof -i :22
taskset
set or retrieve a process’s CPU affinity
# 显示当前进程的 affinity
taskset -c -p 1162
# 设置进程的affinity
taskset -c 0,1 -p 1162
taskset -c 1
proc
Process information pseudo-filesystem
man proc
/proc/<pid/status
/proc/<pid/statm #Memory
valgrind
查内存泄漏
软件调试类
- strace/dtruss: 系统调用跟踪工具
- ltrace:查询库调用
$ strace -p <pid> -c
-p 指定进程ID
-c 汇总输出
辅助类
- pv : 管道进度监控
# 相当于cat
# 监控某进程文件
pv -d <pid>
# 管道命名
pv -cN gzip