linux故障排查
linux查看磁盘io使用情况
常见的linux性能调优工具
- bcc
- bpftrace
- ply
- diagnose-tools
- libbpf CO-RE
查看磁盘容量
df命令
ubuntu@ubuntu-desktop:~$ df --help
用法:df [选项]... [文件]...
显示每个指定文件所在的文件系统的信息,默认是显示所有文件系统。
必选参数对长短选项同时适用。
-a, --all 包含虚拟、重复和无法访问的文件系统
-B, --block-size=大小 使用指定字节数的块。例如,'-BM' 将以
1,048,576 字节为单位显示大小。
参见 SIZE 格式。
-h, --human-readable 以 1024 为基底显示大小(例如:1023M)
-H, --si 以 1000 为基底显示大小(例如,1.1G)
-i, --inodes 显示inode 信息而非块使用量
-k 即--block-size=1K
-l, --local 只显示本机的文件系统
--no-sync 取得使用量数据前不进行同步动作(默认)
--output[=域列表] 使用给定域列表定义的输出格式,
或者在缺省情况下输出所有域。
-P, --portability 使用 POSIX 兼容的输出格式
--sync 取得使用量数据前先调用同步(sync)动作
--total 省略所有对可用空间无显著影响的项并生成总计值
-t, --type=类型 只显示指定文件系统为指定类型的信息
-T, --print-type 显示文件系统类型
-x, --exclude-type=类型 只显示文件系统不是指定类型的信息
-v (忽略)
--help 显示此帮助信息并退出
--version 显示版本信息并退出
一般使用df -h
查看磁盘io情况
iostat
ubuntu@ubuntu-desktop:~$ iostat --help
用法: iostat [ 选项 ] [ <时间间隔> [ <次数> ] ]
选项:
[ -c ] [ -d ] [ -h ] [ -k | -m ] [ -N ] [ -s ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]
[ -j { ID | LABEL | PATH | UUID | ... } ]
[ --dec={ 0 | 1 | 2 } ] [ --human ] [ -o JSON ]
[ [ -H ] -g <用户组名> ] [ -p [ <设备> [,...] | ALL ] ]
[ <设备> [...] | ALL ]
一般用-x
用法示例:
ubuntu@ubuntu-desktop:~$ iostat -h 1 10
Linux 5.11.0-34-generic (ubuntu-desktop) 2021年09月15日 _x86_64_ (16 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
4.3% 0.1% 0.8% 0.6% 0.0% 94.3%
tps kB_read/s kB_wrtn/s kB_dscd/s kB_read kB_wrtn kB_dscd Device
0.05 1.1k 0.0k 0.0k 2.0M 0.0k 0.0k dm-0
0.05 1.1k 0.0k 0.0k 2.0M 0.0k 0.0k dm-1
0.05 1.1k 0.0k 0.0k 2.0M 0.0k 0.0k dm-2
.......
.......
#10次
每1
秒刷新一次,一共刷新10
次。
ubuntu@ubuntu-desktop:~$ iostat -x 1 10
Linux 5.11.0-34-generic (ubuntu-desktop) 2021年09月15日 _x86_64_ (16 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
4.29 0.05 0.78 0.59 0.00 94.29
Device r/s rkB/s rrqm/s %rrqm r_await rareq-sz w/s wkB/s wrqm/s %wrqm w_await wareq-sz d/s dkB/s drqm/s %drqm d_await dareq-sz aqu-sz %util
dm-0 0.05 1.10 0.00 0.00 0.04 21.75 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 0.00
dm-1 0.05 1.10 0.00 0.00 0.08 21.75 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 0.00
dm-2 0.05 1.10 0.00 0.00 0.04 21.75 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 0.00
loop0 0.03 0.57 0.00 0.00 0.09 19.21 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 0.00
loop1 0.03 0.57 0.00 0.00 0.18 17.87 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 0.00
loop10 0.03 0.58 0.00 0.00 0.11 17.90 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 0.00
loop11 0.51 6.67 0.00 0.00 0.05 13.17 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 0.05
loop12 0.03 0.57 0.00 0.00 0.11 20.47 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 0.00
loop13 0.03 0.58 0.00 0.00 0.14 18.73 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 0.00
loop14 0.29 3.45 0.00 0.00 0.06 11.79 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 0.03
loop15 0.03 0.60 0.00 0.00 0.10 19.36 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 0.00
loop16 0.43 14.23 0.00 0.00 0.07 32.76 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 0.08
loop17 0.67 2.25 0.00 0.00 0.02 3.34 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 0.02
loop18 0.04 0.59 0.00 0.00 0.10 14.32 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 0.00
loop19 0.03 0.61 0.00 0.00 0.10 19.35 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 0.00
loop2 0.03 0.57 0.00 0.00 0.19 18.51 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 0.00
loop20 0.03 0.59 0.00 0.00 0.10 19.19 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 0.00
loop21 0.03 0.58 0.00 0.00 0.10 19.09 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 0.00
loop22 0.03 0.58 0.00 0.00 0.15 18.11 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 0.00
loop23 0.03 0.57 0.00 0.00 0.17 18.07 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 0.00
loop24 0.03 0.19 0.00 0.00 0.12 7.30 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 0.00
loop25 0.03 0.56 0.00 0.00 0.12 19.16 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 0.00
loop26 0.03 0.57 0.00 0.00 0.13 18.12 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 0.00
loop27 0.57 17.22 0.00 0.00 0.06 30.16 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 0.09
loop28 0.04 0.70 0.00 0.00 0.01 17.92 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 0.00
loop3 0.03 0.57 0.00 0.00 0.18 19.16 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 0.00
loop4 0.03 0.57 0.00 0.00 0.21 17.19 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 0.00
loop5 0.01 0.03 0.00 0.00 0.00 2.55 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 0.00
loop6 0.52 4.14 0.00 0.00 0.04 7.99 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 0.03
loop7 0.03 0.56 0.00 0.00 0.10 17.35 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 0.00
loop8 0.03 0.59 0.00 0.00 0.13 18.75 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 0.00
loop9 0.02 0.18 0.00 0.00 0.07 7.98 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 0.00
nvme0n1 0.30 9.49 0.39 56.39 0.10 31.35 0.00 0.00 0.00 0.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
nvme1n1 20.25 1060.79 4.48 18.10 0.16 52.37 556.85 8859.18 1222.29 68.70 0.31 15.91 0.00 0.00 0.00 0.00 0.00 0.00 0.22 12.60
sda 0.19 5.09 0.00 0.00 4.93 26.72 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 0.04
......
......
#10次
iotop
ubuntu@ubuntu-desktop:~$ 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)
--no-help suppress listing of shortcuts
查看CPU使用情况
mpstat
mpstat可以查看所有的CPU使用情况。
mpstat -P ALL 2
top
top
查看进程情况
ps
ubuntu@ubuntu-desktop:~$ ps --help all
用法:
ps [选项]
基本选项:
-A, -e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N, --deselect negate selection
r only running processes
T all processes on this terminal
x processes without controlling ttys
Selection by list:
-C <命令> 命令名称
-G, --Group <GID> real group id or name
-g, --group <group> session or effective group name
-p, p, --pid <PID> 进程编号
--ppid <PID> 父进程编号
-q, q, --quick-pid <PID>
process id (quick mode)
-s, --sid <session> session id
-t, t, --tty <tty> 终端
-u, U, --user <UID> effective user id or name
-U, --User <UID> 真实用户编号或名称
The selection options take as their argument either:
a comma-separated list e.g. '-u root,nobody' or
a blank-separated list e.g. '-p 123 4567'
输出格式:
-F extra full
-f 完整格式,包含命令行
f, --forest ascii 字符艺术显示的进程树
-H show process hierarchy
-j jobs format
j BSD 任务控制格式
-l 长格式
l BSD 长格式
-M, Z 添加安全数据(用于 SELinux)
-O <format> preloaded with default columns
O <format> as -O, with BSD personality
-o, o, --format <格式>
用户定义的格式
s 信号格式
u user-oriented format
v 虚拟内存格式
X 寄存器格式
-y do not show flags, show rss vs. addr (used with -l)
--context 显示安全上下文(用于 SELinux)
--headers repeat header lines, one per page
--no-headers do not print header at all
--cols, --columns, --width <num>
set screen width
--rows, --lines <num>
set screen height
Show threads:
H as if they were processes
-L possibly with LWP and NLWP columns
-m, m after processes
-T possibly with SPID column
Miscellaneous options:
-c show scheduling class with -l option
c show true command name
e show the environment after command
k, --sort specify sort order as: [+|-]key[,[+|-]key[,...]]
L show format specifiers
n display numeric uid and wchan
S, --cumulative include some dead child process data
-y do not show flags, show rss (only with -l)
-V, V, --version display version information and exit
-w, w 不限制输出宽度
一般使用示例:
#按第三列排序
ubuntu@ubuntu-desktop:~$ ps -aux | head -1; ps -aux | sort -rn -k +3 | head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
ubuntu 3565 19.5 2.6 3244744 437944 ? Sl 19:12 5:12 /opt/netease/netease-cloud-music/netease-cloud-music --type=zygote --no-sandbox --lang=zh-CN --log-file=/home/ubuntu/.cache/netease-cloud-music/Cef/console.log --log-severity=error
ubuntu 2689 18.9 3.1 7069480 513404 ? Ssl 19:12 5:03 /usr/bin/gnome-shell
ubuntu 3389 12.6 1.4 8963544 229788 ? Sl 19:12 3:22 /opt/netease/netease-cloud-music/netease-cloud-music
ubuntu 6750 11.9 5.5 6195404 908584 ? Sl 19:16 2:44 /usr/lib/firefox/firefox -new-window
root 2258 8.3 1.1 2756308 195080 tty2 Sl+ 19:12 2:14 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
ubuntu 7156 4.9 1.2 2645336 209888 ? Sl 19:16 1:07 /usr/lib/firefox/firefox -contentproc -childID 5 -isForBrowser -prefsLen 8501 -prefMapSize 243188 -jsInit 285716 -parentBuildID 20210903235534 -appdir /usr/lib/firefox/browser 6750 true tab
gdm 1927 2.0 1.5 6927084 249816 tty1 Sl+ 19:12 0:32 /usr/bin/gnome-shell
ubuntu 7463 1.1 0.4 908764 70220 ? Ssl 19:17 0:15 /usr/libexec/gnome-terminal-server
ubuntu 6933 0.5 0.8 2532200 142672 ? Sl 19:16 0:07 /usr/lib/firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 5003 -prefMapSize 243188 -jsInit 285716 -parentBuildID 20210903235534 -appdir /usr/lib/firefox/browser 6750 true tab
ubuntu 2898 0.5 1.0 1235724 171352 ? Sl 19:12 0:08 /usr/bin/nautilus --gapplication-service
buntu@ubuntu-desktop:~$ ps -xef
PID TTY STAT TIME COMMAND
2256 tty2 Ssl+ 0:00 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu /usr/bin/gnome-session --systemd -
2364 tty2 Sl+ 0:00 \_ /usr/libexec/gnome-session-binary --systemd --systemd --session=ubuntu LANGUAGE=zh_CN:zh:en_US:en USER=ubun
2474 ? Ss 0:00 \_ /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu /usr/bin/gnome-session --syst
2483 tty2 Z+ 0:00 \_ [fcitx] <defunct>
2760 ? Sl 0:03 /opt/sogoupinyin/files/bin/sogoupinyinService LANGUAGE=zh_CN:zh:en_US:en USER=ubuntu LC_TIME=zh_CN.UTF-8 XDG_SE
2565 ? SN 0:00 /usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-y71MdyaA4G,guid=f74d19dc7b13e1905e65e4486141d52c 2493 LANGU
2493 ? Ss 0:00 /usr/bin/dbus-daemon --syslog --fork --print-pid 5 --print-address 7 --config-file /usr/share/fcitx/dbus/daemon
2485 ? Sl 0:01 /usr/bin/fcitx -d LANGUAGE=zh_CN:zh:en_US:en USER=ubuntu LC_TIME=zh_CN.UTF-8 XDG_SEAT=seat0 XDG_SESSION_TYPE=x1
2176 ? Sl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
2160 ? Ss 0:00 /lib/systemd/systemd --user LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh:en_US:en LC_ADDRESS=zh_CN.UTF-8 LC_IDENTIFICATIO
2161 ? S 0:00 \_ (sd-pam)
2166 ? S<sl 0:00 \_ /usr/bin/pulseaudio --daemonize=no --log-target=journal HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:z
2168 ? SNsl 0:01 \_ /usr/libexec/tracker-miner-fs HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh:en_US:en LC_ADDRESS=zh_C
2172 ? Ss 0:00 \_ /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only H
2179 ? Ssl 0:00 \_ /usr/libexec/gvfsd HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh:en_US:en LC_ADDRESS=zh_CN.UTF-8 LC_
2912 ? Sl 0:00 | \_ /usr/libexec/gvfsd-trash --spawner :1.3 /org/gtk/gvfs/exec_spaw/0 HOME=/home/ubuntu LANG=zh_CN.UTF-8 LA
2184 ? Sl 0:00 \_ /usr/libexec/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh
2203 ? Ssl 0:00 \_ /usr/libexec/gvfs-udisks2-volume-monitor HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh:en_US:en LC_A
2213 ? Ssl 0:00 \_ /usr/libexec/gvfs-goa-volume-monitor HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh:en_US:en LC_ADDRE
2218 ? Sl 0:00 \_ /usr/libexec/goa-daemon SHELL=/bin/bash DBUS_STARTER_ADDRESS=unix:path=/run/user/1000/bus,guid=add5d04fd3c5
2226 ? Sl 0:00 \_ /usr/libexec/goa-identity-service SHELL=/bin/bash DBUS_STARTER_ADDRESS=unix:path=/run/user/1000/bus,guid=ad
2231 ? Ssl 0:00 \_ /usr/libexec/gvfs-mtp-volume-monitor HOME=/home/ubuntu LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh:en_US:en LC_ADDRE
ps -aux | head -1
表示输出表头
查看内存使用情况
top命令
ps -aux命令
ps -aux配合awk命令统计各进程总计内存占用情况
ps -aux | grep docker | awk ‘{a+=$6} END {print a}’
systemctl status docker也可以查看服务总占用内存
ps -aux | sort -nr -k 6 | head -10
sort -n 按数值大小排序(默认从小到大)
sort -r 反向排序(从大到小)
sort -k 6 按第六列
-k可以多个,例如
sort -k 1 -k 2先按第一列,然后按第二列。
smem工具
smem | grep dockerd
smem | grep docker-containerd-cshim | awk ‘{a+=$6} END {print a}’
smem | grep docker-containerd-cshim | awk ‘{a+=$7} END {print a}’
查看僵尸进程
ps -aux | grep -e '^[Zz]'
docker保存函数调用堆栈信息
docker
ubuntu@ubuntu-desktop:~$ kill -s USR1 $(pidof dockerd)
日志保存在/run/docker中
如果是snap安装的docker,则使用snap logs docker
命令查看docker的日志位置。
堆栈日志保存在/run/snap.docker/文件夹下。
如下所示:
root@ubuntu-desktop:~# snap logs docker
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.893968055+08:00" level=debug msg="Registering GET, /networks/"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.893984950+08:00" level=debug msg="Registering GET, /networks/{id:.+}"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.894009031+08:00" level=debug msg="Registering POST, /networks/create"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.894028382+08:00" level=debug msg="Registering POST, /networks/{id:.*}/connect"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.894051996+08:00" level=debug msg="Registering POST, /networks/{id:.*}/disconnect"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.894082554+08:00" level=debug msg="Registering POST, /networks/prune"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.894102513+08:00" level=debug msg="Registering DELETE, /networks/{id:.*}"
2021-09-16T14:00:33Z docker.dockerd[15666]: time="2021-09-16T22:00:33.894183330+08:00" level=info msg="API listen on /var/run/docker.sock"
2021-09-16T14:00:33Z docker.dockerd[15737]: time="2021-09-16T22:00:33.902900434+08:00" level=debug msg="garbage collected" d="623.878µs"
2021-09-16T14:01:25Z docker.dockerd[15666]: time="2021-09-16T22:01:25.589316392+08:00" level=info msg="goroutine stacks written to /run/snap.docker/goroutine-stacks-2021-09-16T220125+0800.log"
系统资源监控
sar
如果报错,没有那个文件或目录
,意思是没有创建sar日志目录,使用命令
sar -o 2 15
2 表示每2秒输出一次,15表示当天日期。
head命令
用法:head [选项]... [文件]...
将每个指定文件的前 10 行输出到标准输出。
如果指定了多于一个文件,在每块输出之前附加文件名称作为头部。
如果没有指定文件,或者文件为"-",则从标准输入读取。
必选参数对长短选项同时适用。
-c, --bytes=[-]K 显示每个文件的前 K 字节内容;
如果数字前附加“-”字符,则除了每个文件的
最后 K 字节数据外显示其余全部内容
-n, --lines=[-]K 显示每个文件的前 K 行内容而非前 10 行内容;
如果数字前附加“-”字符,则除了每个文件的
最后 K 行数据外显示其余全部内容
-q, --quiet, --silent 不显示包含给定文件名的文件头
-v, --verbose 总是显示包含给定文件名的文件头
-z, --zero-terminated 以 NUL 字符而非换行符作为行尾分隔符
--help 显示此帮助信息并退出
--version 显示版本信息并退出
sort命令
ubuntu@ubuntu-desktop:~$ sort --help
用法:sort [选项]... [文件]...
或:sort [选项]... --files0-from=F
串联排序所有指定文件并将结果写到标准输出。
如果没有指定文件,或者文件为"-",则从标准输入读取。
必选参数对长短选项同时适用。
排序选项:
-b, --ignore-leading-blanks 忽略前导的空白区域
-d, --dictionary-order 只考虑空白区域和字母字符
-f, --ignore-case 忽略字母大小写
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < 'JAN' < ... < 'DEC'
-h, --human-numeric-sort 使用易读性数字(例如: 2K 1G)
-n, --numeric-sort compare according to string numerical value
-R, --random-sort shuffle, but group identical keys. See shuf(1)
--random-source=FILE get random bytes from FILE
-r, --reverse reverse the result of comparisons
--sort=WORD 按照WORD 指定的格式排序:
一般数字-g,高可读性-h,月份-M,数字-n,
随机-R,版本-V
-V, --version-sort 在文本内进行自然版本排序
其他选项:
--batch-size=NMERGE 一次最多合并NMERGE 个输入;如果输入更多
则使用临时文件
-c, --check, --check=diagnose-first 检查输入是否已排序,若已有序则不进行操作
-C, --check=quiet, --check=silent 类似-c,但不报告第一个无序行
--compress-program=程序 使用指定程序压缩临时文件;使用该程序
的-d 参数解压缩文件
--debug 为用于排序的行添加注释,并将有可能有问题的
用法输出到标准错误输出
--files0-from=文件 从指定文件读取以NUL 终止的名称,如果该文件被
指定为"-"则从标准输入读文件名
-k, --key=KEYDEF sort via a key; KEYDEF gives location and type
-m, --merge merge already sorted files; do not sort
-o, --output=文件 将结果写入到文件而非标准输出
-s, --stable 禁用last-resort 比较以稳定比较算法
-S, --buffer-size=大小 指定主内存缓存大小
-t, --field-separator=分隔符 使用指定的分隔符代替非空格到空格的转换
-T, --temporary-directory=目录 使用指定目录而非$TMPDIR 或/tmp 作为
临时目录,可用多个选项指定多个目录
--parallel=N 将同时运行的排序数改变为N
-u, --unique 配合-c,严格校验排序;不配合-c,则只输出一次排序结果
-z, --zero-terminated 以 NUL 空字符而非换行符作为行尾分隔符
--help 显示此帮助信息并退出
--version 显示版本信息并退出
一般用法sort -rn -k +3
。表示按照第三列,数字降序排列。
tail命令
tail 命令可用于查看文件的内容,有一个常用的参数-f
常用于查阅正在改变的日志文件。
ubuntu@ubuntu-desktop:~$ tail --help
用法:tail [选项]... [文件]...
显示每个指定文件的最后 10 行并输出至标准输出。
若指定了多于一个文件,程序会在每段输出的开始添加相应文件名作为头。
如果没有指定文件,或者文件为"-",则从标准输入读取。
必选参数对长短选项同时适用。
-c, --bytes=[+]NUM 输出最后 NUM 字节;或者使用 -c +NUM 以输出
每个文件第 NUM 个字节之后的全部内容
-f, --follow[={name|descriptor}]
随文件增长即时输出新增数据;
若未给定选项参数,则默认使用 'descriptor'
-F 和 --follow=name --retry 相同
-n, --lines=[+]NUM output the last NUM lines, instead of the last 10;
or use -n +NUM to output starting with line NUM
--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to see if it has been unlinked or renamed
(this is the usual case of rotated log files);
with inotify, this option is rarely useful
--pid=PID 同 -f 一起使用,当所指定<PID>对应的进程死去后终止
-q, --quiet, --silent 不输出给出文件名的头
--retry 即使目标文件不可访问依然重复试图打开
-s, --sleep-interval=N with -f, sleep for approximately N seconds
(default 1.0) between iterations;
with inotify and --pid=P, check process P at
least once every N seconds
-v, --verbose always output headers giving file names
-z, --zero-terminated 以 NUL 字符而非换行符作为行尾分隔符
--help 显示此帮助信息并退出
--version 显示版本信息并退出
ubuntu@ubuntu-desktop:~$ tail -f /var/log/messages
查看系统内所有的用户(user)
compgen命令(bash自带的命令)
$ compgen -u
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
ftp
postfix
sshd
tcpdump
2gadmin
apache
zabbix
mysql
zend
rpc
2daygeek
named
mageshm
查看系统上的用户组(group)
compgen命令(bash自带)
$ compgen -g
shell脚本统计某个命令耗时
declare -a times
for ((i=1; i<100; i++));
do
startTime=$(date +%s%N)
# 命令
endTime=$(date +%s%N)
costTime=$((endTime-startTime))
echo $costTime
times[$i]=$(echo $costTime | awk '{printf "%.2f\n", $0/1000000000}')
echo ${times[$i]}
done
查看mount
mount
查看所有磁盘
fdisk -l
查看硬盘分区
lsblk -f
内存统计脚本
#!/usr/bin/env bash
uss=`smem|grep docker-containerd-.shim |awk '{print $6}'`
count=0
total_uss_mem=0
echo $uss
for u in ${uss[*]}
do
count=$[$count + 1]
total_uss_mem=$[$total_uss_mem + $u]
done
echo "Total_USS_MEM=" $total_uss_mem
echo "Average_USS_MEM=" `expr $total_uss_mem / $count`
echo "............................................."
pss=`smem|grep docker-containerd-.shim |awk '{print $7}'`
count=0
total_pss_mem=0
echo $pss
for p in ${pss[*]}
do
count=$[$count + 1]
total_pss_mem=$[$total_pss_mem + $p]
done
echo "Total_pss_MEM=" $total_pss_mem
echo "Average_PSS_MEM=" `expr $total_pss_mem / $count`
echo "............................................."
统计命令执行耗时和CPU/内存消耗
python脚本如下:
# -*- coding:utf-8 -*-
import sys
import os
import time
import threading
def cmdLine(command, eventTop, eventFor):
eventTop.set()
os.system(command=command)
eventTop.clear()
eventFor.set()
pass
def cmdTOP(interval, eventTop, eventMain):
command = "top -p $(pidof dockerd) -bn1 | grep $(pidof dockerd)"
#command = "ps -aux | grep dockerd | grep $(pidof dockerd)"
cmdReturn = []
cpuCosts = []
eventTop.wait()
#title = os.popen("top -p $(pidof dockerd) -bn1").readlines()[-2].split()
#print(title)
while (eventMain.is_set() == True):
cmdReturnObject = os.popen(command)
cmdReturn.append(cmdReturnObject.readlines()[0].split())
#print(cmdReturn)
#time.sleep(interval)
#for i, val in enumerate(cmdReturn):
for val in cmdReturn:
#print(i, val)
cpuCost = val[8]
cpuCosts.append(cpuCost)
pass # end for
pass# end while
print("cpuCosts =", cpuCosts)
return
def cmdMEM(interval, eventTop, eventMain):
commandMEM = "ps -aux | grep dockerd | grep $(pidof dockerd)"
cmdReturn = []
memCosts = []
eventTop.wait()
#title = os.popen("top -p $(pidof dockerd) -bn1").readlines()[-2].split()
#print(title)
while (eventMain.is_set() == True):
cmdReturnObject = os.popen(commandMEM)
cmdReturn.append(cmdReturnObject.readlines()[0].split())
#print(cmdReturn)
time.sleep(interval)
#for i, val in enumerate(cmdReturn):
for val in cmdReturn:
#print(i, val)
memCost = val[5]
memCosts.append(memCost)
pass # end for
pass# end while
print("memCosts =", memCosts)
return
def cal(num, command, interval):
totalTime = 0
eventTop = threading.Event()
eventFor = threading.Event()
eventMain = threading.Event()
eventMain.set()
threadTOP = threading.Thread(target=cmdTOP, args=(interval, eventTop, eventMain))
threadTOP.start()
threadTOP = threading.Thread(target=cmdMEM, args=(interval, eventTop, eventMain))
threadTOP.start()
for _ in range(num):
startTime = time.time()
threadCMD = threading.Thread(target=cmdLine, args=(command, eventTop, eventFor))
threadCMD.start()
eventFor.wait()
eventFor.clear()
endTime = time.time()
totalTime = totalTime + endTime - startTime
pass
eventMain.clear()
print("totalTime =", totalTime, "s")
averageTime = totalTime / num
print("averageTime =", averageTime, "s")
return
def calculateTime(num, command, event):
startTime = time.time()
for _ in range(num):
os.system(command)
pass
event.set()
endTime = time.time()
totalTime = endTime - startTime
print("totalTime =", totalTime, "s")
averageTime = totalTime / num
print("averageTime =", averageTime, "s")
return None
def calCPUcost(interval, event):
#command = "top -p $(pidof dockerd)"
cmdReturn = []
cpuCosts = []
#title = os.popen("top -p $(pidof dockerd) -bn1").readlines()[-2].split()
#print(title)
while (event.is_set() == False):
command = "top -p $(pidof dockerd) -bn1 | grep $(pidof dockerd)"
cmdReturnObject = os.popen(command)
cmdReturn.append(cmdReturnObject.readlines()[0].split())
#print(cmdReturn)
time.sleep(interval)
pass
#for i, val in enumerate(cmdReturn):
for val in cmdReturn:
#print(i, val)
cpuCost = val[8]
cpuCosts.append(cpuCost)
pass
print(cpuCosts)
return
def main():
interval = 0.5
lenth = len(sys.argv)
if lenth <= 1:
print("请输入docker或者curl命令!")
return
try:
num = int(sys.argv[1])
pass
except :
print("第一个参数为循环执行次数,请输入一个正整数!")
return
command = ""
for i in range(2, lenth):
command = command + sys.argv[i] + " "
pass
os.system(command=command)
print()
command = command + "> /dev/null 2>&1"
print(command)
# event = threading.Event()
# thread_calTime = threading.Thread(target=calculateTime, args=(num, command, event))
# thread_calTime.start()
# thread_calCPUcost = threading.Thread(target=calCPUcost, args=(interval, event))
# thread_calCPUcost.start()
cal(num, command, interval)
time.sleep(1)
return
main()