系统相关
首页 > 系统相关> > 51.第十一章 进程和计划任务(三)

51.第十一章 进程和计划任务(三)

作者:互联网

2.10 进程对应的内存映射 pmap

格式:

pmap [options] pid [...]

常用选项

-x: 显示详细格式的信息

范例:

[root@rocky8 ~]# pmap 1
1:   /usr/lib/systemd/systemd --switched-root --system --deserialize 17
000055b2f93c0000   1288K r-x-- systemd
000055b2f9701000    244K r---- systemd
000055b2f973e000      4K rw--- systemd
000055b2fb61d000   3796K rw---   [ anon ]
00007f5244000000    132K rw---   [ anon ]
00007f5244021000  65404K -----   [ anon ]
00007f524b7ae000      4K -----   [ anon ]
00007f524b7af000   8192K rw---   [ anon ]
00007f524bfaf000      4K -----   [ anon ]
00007f524bfb0000   8192K rw---   [ anon ]
00007f524c7b0000    132K r-x-- libnl-3.so.200.26.0
00007f524c7d1000   2044K ----- libnl-3.so.200.26.0
00007f524c9d0000      8K r---- libnl-3.so.200.26.0
00007f524c9d2000      4K rw--- libnl-3.so.200.26.0
00007f524c9d3000    492K r-x-- libnl-route-3.so.200.26.0
00007f524ca4e000   2048K ----- libnl-route-3.so.200.26.0
00007f524cc4e000     16K r---- libnl-route-3.so.200.26.0
00007f524cc52000     20K rw--- libnl-route-3.so.200.26.0
00007f524cc57000      8K rw---   [ anon ]
00007f524cc59000    120K r-x-- libibverbs.so.1.11.32.0
00007f524cc77000   2044K ----- libibverbs.so.1.11.32.0
00007f524ce76000      4K r---- libibverbs.so.1.11.32.0
00007f524ce77000      4K rw--- libibverbs.so.1.11.32.0
00007f524ce78000   1540K r-x-- libm-2.28.so
00007f524cff9000   2044K ----- libm-2.28.so
00007f524d1f8000      4K r---- libm-2.28.so
00007f524d1f9000      4K rw--- libm-2.28.so
00007f524d1fa000    580K r-x-- libudev.so.1.6.11
00007f524d28b000   2048K ----- libudev.so.1.6.11
00007f524d48b000     16K r---- libudev.so.1.6.11
00007f524d48f000      4K rw--- libudev.so.1.6.11
00007f524d490000    696K r-x-- libsepol.so.1
00007f524d53e000   2048K ----- libsepol.so.1
00007f524d73e000      4K r---- libsepol.so.1
00007f524d73f000      4K rw--- libsepol.so.1
00007f524d740000      8K rw---   [ anon ]
00007f524d742000   1524K r-x-- libunistring.so.2.1.0
00007f524d8bf000   2044K ----- libunistring.so.2.1.0
00007f524dabe000     16K r---- libunistring.so.2.1.0
00007f524dac2000      4K rw--- libunistring.so.2.1.0
00007f524dac3000    288K r-x-- libpcap.so.1.9.1
00007f524db0b000   2048K ----- libpcap.so.1.9.1
00007f524dd0b000      8K r---- libpcap.so.1.9.1
00007f524dd0d000      4K rw--- libpcap.so.1.9.1
00007f524dd0e000    124K r-x-- libgpg-error.so.0.24.2
00007f524dd2d000   2048K ----- libgpg-error.so.0.24.2
00007f524df2d000      4K r---- libgpg-error.so.0.24.2
00007f524df2e000      4K rw--- libgpg-error.so.0.24.2
00007f524df2f000     56K r-x-- libjson-c.so.4.0.0
00007f524df3d000   2048K ----- libjson-c.so.4.0.0
00007f524e13d000      4K r---- libjson-c.so.4.0.0
00007f524e13e000      4K rw--- libjson-c.so.4.0.0
00007f524e13f000    340K r-x-- libdevmapper.so.1.02
00007f524e194000   2044K ----- libdevmapper.so.1.02
00007f524e393000      8K r---- libdevmapper.so.1.02
00007f524e395000     12K rw--- libdevmapper.so.1.02
00007f524e398000      4K rw---   [ anon ]
00007f524e399000     16K r-x-- libattr.so.1.1.2448
00007f524e39d000   2048K ----- libattr.so.1.1.2448
00007f524e59d000      4K r---- libattr.so.1.1.2448
00007f524e59e000      4K rw---   [ anon ]
00007f524e59f000   2764K r-x-- libcrypto.so.1.1.1g
00007f524e852000   2044K ----- libcrypto.so.1.1.1g
00007f524ea51000    176K r---- libcrypto.so.1.1.1g
00007f524ea7d000     16K rw--- libcrypto.so.1.1.1g
00007f524ea81000     16K rw---   [ anon ]
00007f524ea85000    540K r-x-- libssl.so.1.1.1g
00007f524eb0c000   2044K ----- libssl.so.1.1.1g
00007f524ed0b000     36K r---- libssl.so.1.1.1g
00007f524ed14000     16K rw--- libssl.so.1.1.1g
00007f524ed18000      4K rw---   [ anon ]
00007f524ed19000     88K r-x-- libz.so.1.2.11
00007f524ed2f000   2044K ----- libz.so.1.2.11
00007f524ef2e000      4K r---- libz.so.1.2.11
00007f524ef2f000      4K rw---   [ anon ]
00007f524ef30000     20K r-x-- libcap-ng.so.0.0.0
00007f524ef35000   2044K ----- libcap-ng.so.0.0.0
00007f524f134000      4K r---- libcap-ng.so.0.0.0
00007f524f135000      4K rw--- libcap-ng.so.0.0.0
00007f524f136000     24K r-x-- libuuid.so.1.3.0
00007f524f13c000   2048K ----- libuuid.so.1.3.0
00007f524f33c000      4K r---- libuuid.so.1.3.0
00007f524f33d000      4K rw---   [ anon ]
00007f524f33e000     12K r-x-- libdl-2.28.so
00007f524f341000   2044K ----- libdl-2.28.so
00007f524f540000      4K r---- libdl-2.28.so
00007f524f541000      4K rw--- libdl-2.28.so
00007f524f542000    524K r-x-- libpcre2-8.so.0.7.1
00007f524f5c5000   2044K ----- libpcre2-8.so.0.7.1
00007f524f7c4000      4K r---- libpcre2-8.so.0.7.1
00007f524f7c5000      4K rw--- libpcre2-8.so.0.7.1
00007f524f7c6000    304K r-x-- libblkid.so.1.1.0
00007f524f812000   2048K ----- libblkid.so.1.1.0
00007f524fa12000     20K r---- libblkid.so.1.1.0
00007f524fa17000      4K rw--- libblkid.so.1.1.0
00007f524fa18000      4K rw---   [ anon ]
00007f524fa19000    112K r-x-- liblz4.so.1.8.3
00007f524fa35000   2044K ----- liblz4.so.1.8.3
00007f524fc34000      4K r---- liblz4.so.1.8.3
00007f524fc35000      4K rw---   [ anon ]
00007f524fc36000    148K r-x-- liblzma.so.5.2.4
00007f524fc5b000   2048K ----- liblzma.so.5.2.4
00007f524fe5b000      4K r---- liblzma.so.5.2.4
00007f524fe5c000      4K rw---   [ anon ]
00007f524fe5d000    112K r-x-- libidn2.so.0.3.6
00007f524fe79000   2048K ----- libidn2.so.0.3.6
00007f5250079000      4K r---- libidn2.so.0.3.6
00007f525007a000      4K rw---   [ anon ]
00007f525007b000     24K r-x-- libip4tc.so.2.0.0
00007f5250081000   2048K ----- libip4tc.so.2.0.0
00007f5250281000      4K r---- libip4tc.so.2.0.0
00007f5250282000      4K rw--- libip4tc.so.2.0.0
00007f5250283000   1120K r-x-- libgcrypt.so.20.2.5
00007f525039b000   2044K ----- libgcrypt.so.20.2.5
00007f525059a000      8K r---- libgcrypt.so.20.2.5
00007f525059c000     20K rw--- libgcrypt.so.20.2.5
00007f52505a1000    476K r-x-- libcryptsetup.so.12.6.0
00007f5250618000   2044K ----- libcryptsetup.so.12.6.0
00007f5250817000      8K r---- libcryptsetup.so.12.6.0
00007f5250819000      8K rw--- libcryptsetup.so.12.6.0
00007f525081b000     32K r-x-- libacl.so.1.1.2253
00007f5250823000   2044K ----- libacl.so.1.1.2253
00007f5250a22000      4K r---- libacl.so.1.1.2253
00007f5250a23000      4K rw--- libacl.so.1.1.2253
00007f5250a24000     16K r-x-- libcap.so.2.26
00007f5250a28000   2048K ----- libcap.so.2.26
00007f5250c28000      4K r---- libcap.so.2.26
00007f5250c29000      4K rw--- libcap.so.2.26
00007f5250c2a000   1776K r-x-- libc-2.28.so
00007f5250de6000   2044K ----- libc-2.28.so
00007f5250fe5000     16K r---- libc-2.28.so
00007f5250fe9000      8K rw--- libc-2.28.so
00007f5250feb000     16K rw---   [ anon ]
00007f5250fef000    108K r-x-- libpthread-2.28.so
00007f525100a000   2044K ----- libpthread-2.28.so
00007f5251209000      4K r---- libpthread-2.28.so
00007f525120a000      4K rw--- libpthread-2.28.so
00007f525120b000     16K rw---   [ anon ]
00007f525120f000     92K r-x-- libgcc_s-8-20200928.so.1
00007f5251226000   2044K ----- libgcc_s-8-20200928.so.1
00007f5251425000      4K r---- libgcc_s-8-20200928.so.1
00007f5251426000      4K rw--- libgcc_s-8-20200928.so.1
00007f5251427000     92K r-x-- libkmod.so.2.3.3
00007f525143e000   2044K ----- libkmod.so.2.3.3
00007f525163d000      4K r---- libkmod.so.2.3.3
00007f525163e000      4K rw--- libkmod.so.2.3.3
00007f525163f000    116K r-x-- libaudit.so.1.0.0
00007f525165c000   2044K ----- libaudit.so.1.0.0
00007f525185b000      4K r---- libaudit.so.1.0.0
00007f525185c000      4K rw--- libaudit.so.1.0.0
00007f525185d000     48K rw---   [ anon ]
00007f5251869000     56K r-x-- libpam.so.0.84.2
00007f5251877000   2048K ----- libpam.so.0.84.2
00007f5251a77000      4K r---- libpam.so.0.84.2
00007f5251a78000      4K rw--- libpam.so.0.84.2
00007f5251a79000    340K r-x-- libmount.so.1.1.0
00007f5251ace000   2048K ----- libmount.so.1.1.0
00007f5251cce000     12K r---- libmount.so.1.1.0
00007f5251cd1000      4K rw--- libmount.so.1.1.0
00007f5251cd2000      4K rw---   [ anon ]
00007f5251cd3000    156K r-x-- libselinux.so.1
00007f5251cfa000   2044K ----- libselinux.so.1
00007f5251ef9000      4K r---- libselinux.so.1
00007f5251efa000      4K rw--- libselinux.so.1
00007f5251efb000      8K rw---   [ anon ]
00007f5251efd000    120K r-x-- libseccomp.so.2.5.1
00007f5251f1b000   2048K ----- libseccomp.so.2.5.1
00007f525211b000      4K r---- libseccomp.so.2.5.1
00007f525211c000      4K rw--- libseccomp.so.2.5.1
00007f525211d000     28K r-x-- librt-2.28.so
00007f5252124000   2044K ----- librt-2.28.so
00007f5252323000      4K r---- librt-2.28.so
00007f5252324000      4K rw--- librt-2.28.so
00007f5252325000   2068K r-x-- libsystemd-shared-239.so
00007f525252a000   2048K ----- libsystemd-shared-239.so
00007f525272a000    564K r---- libsystemd-shared-239.so
00007f52527b7000      4K rw--- libsystemd-shared-239.so
00007f52527b8000      8K rw---   [ anon ]
00007f52527ba000    176K r-x-- ld-2.28.so
00007f52529ce000     72K rw---   [ anon ]
00007f52529e4000      8K rw---   [ anon ]
00007f52529e6000      4K r---- ld-2.28.so
00007f52529e7000      8K rw--- ld-2.28.so
00007ffc98722000    132K rw---   [ stack ]
00007ffc9875e000     16K r----   [ anon ]
00007ffc98762000      8K r-x--   [ anon ]
ffffffffff600000      4K r-x--   [ anon ]
 total           186032K

另外一种实现

cat /proc/PID/maps

范例:

[root@rocky8 ~]# echo $BASHPID
10189
[root@rocky8 ~]# pmap 10189
10189:   -bash
0000564515c68000   1056K r-x-- bash
0000564515f6f000     16K r---- bash
0000564515f73000     36K rw--- bash
0000564515f7c000     40K rw---   [ anon ]
00005645167b3000    396K rw---   [ anon ]
00007f79df02d000   2528K r---- LC_COLLATE
00007f79df2a5000     44K r-x-- libnss_files-2.28.so
00007f79df2b0000   2048K ----- libnss_files-2.28.so
00007f79df4b0000      4K r---- libnss_files-2.28.so
00007f79df4b1000      4K rw--- libnss_files-2.28.so
00007f79df4b2000     24K rw---   [ anon ]
00007f79df4b8000   9040K r--s- passwd (deleted)
00007f79dfd8c000     40K r-x-- libnss_sss.so.2
00007f79dfd96000   2044K ----- libnss_sss.so.2
00007f79dff95000      4K r---- libnss_sss.so.2
00007f79dff96000      4K rw--- libnss_sss.so.2
00007f79dff97000   1776K r-x-- libc-2.28.so
00007f79e0153000   2044K ----- libc-2.28.so
00007f79e0352000     16K r---- libc-2.28.so
00007f79e0356000      8K rw--- libc-2.28.so
00007f79e0358000     16K rw---   [ anon ]
00007f79e035c000     12K r-x-- libdl-2.28.so
00007f79e035f000   2044K ----- libdl-2.28.so
00007f79e055e000      4K r---- libdl-2.28.so
00007f79e055f000      4K rw--- libdl-2.28.so
00007f79e0560000    164K r-x-- libtinfo.so.6.1
00007f79e0589000   2044K ----- libtinfo.so.6.1
00007f79e0788000     16K r---- libtinfo.so.6.1
00007f79e078c000      4K rw--- libtinfo.so.6.1
00007f79e078d000    176K r-x-- ld-2.28.so
00007f79e0950000    332K r---- LC_CTYPE
00007f79e09a3000      4K r---- LC_NUMERIC
00007f79e09a4000      4K r---- LC_TIME
00007f79e09a5000      4K r---- LC_MONETARY
00007f79e09a6000      4K r---- SYS_LC_MESSAGES
00007f79e09a7000      4K r---- LC_PAPER
00007f79e09a8000      4K r---- LC_NAME
00007f79e09a9000     28K r--s- gconv-modules.cache
00007f79e09b0000     20K rw---   [ anon ]
00007f79e09b5000      4K r---- LC_ADDRESS
00007f79e09b6000      4K r---- LC_TELEPHONE
00007f79e09b7000      4K r---- LC_MEASUREMENT
00007f79e09b8000      4K r---- LC_IDENTIFICATION
00007f79e09b9000      4K r---- ld-2.28.so
00007f79e09ba000      8K rw--- ld-2.28.so
00007fffaa035000    132K rw---   [ stack ]
00007fffaa0c3000     16K r----   [ anon ]
00007fffaa0c7000      8K r-x--   [ anon ]
ffffffffff600000      4K r-x--   [ anon ]
 total            26248K

[root@rocky8 ~]# cat /proc/10189/maps
564515c68000-564515d70000 r-xp 00000000 08:02 17502                      /usr/bin/bash
564515f6f000-564515f73000 r--p 00107000 08:02 17502                      /usr/bin/bash
564515f73000-564515f7c000 rw-p 0010b000 08:02 17502                      /usr/bin/bash
564515f7c000-564515f86000 rw-p 00000000 00:00 0 
5645167b3000-564516816000 rw-p 00000000 00:00 0                          [heap]
7f79df02d000-7f79df2a5000 r--p 00000000 08:02 134352575                  /usr/lib/locale/en_US.utf8/LC_COLLATE
7f79df2a5000-7f79df2b0000 r-xp 00000000 08:02 67117920                   /usr/lib64/libnss_files-2.28.so
7f79df2b0000-7f79df4b0000 ---p 0000b000 08:02 67117920                   /usr/lib64/libnss_files-2.28.so
7f79df4b0000-7f79df4b1000 r--p 0000b000 08:02 67117920                   /usr/lib64/libnss_files-2.28.so
7f79df4b1000-7f79df4b2000 rw-p 0000c000 08:02 67117920                   /usr/lib64/libnss_files-2.28.so
7f79df4b2000-7f79df4b8000 rw-p 00000000 00:00 0 
7f79df4b8000-7f79dfd8c000 r--s 00000000 08:02 201328529                  /var/lib/sss/mc/passwd (deleted)
7f79dfd8c000-7f79dfd96000 r-xp 00000000 08:02 67750436                   /usr/lib64/libnss_sss.so.2
7f79dfd96000-7f79dff95000 ---p 0000a000 08:02 67750436                   /usr/lib64/libnss_sss.so.2
7f79dff95000-7f79dff96000 r--p 00009000 08:02 67750436                   /usr/lib64/libnss_sss.so.2
7f79dff96000-7f79dff97000 rw-p 0000a000 08:02 67750436                   /usr/lib64/libnss_sss.so.2
7f79dff97000-7f79e0153000 r-xp 00000000 08:02 67117908                   /usr/lib64/libc-2.28.so
7f79e0153000-7f79e0352000 ---p 001bc000 08:02 67117908                   /usr/lib64/libc-2.28.so
7f79e0352000-7f79e0356000 r--p 001bb000 08:02 67117908                   /usr/lib64/libc-2.28.so
7f79e0356000-7f79e0358000 rw-p 001bf000 08:02 67117908                   /usr/lib64/libc-2.28.so
7f79e0358000-7f79e035c000 rw-p 00000000 00:00 0 
7f79e035c000-7f79e035f000 r-xp 00000000 08:02 67117910                   /usr/lib64/libdl-2.28.so
7f79e035f000-7f79e055e000 ---p 00003000 08:02 67117910                   /usr/lib64/libdl-2.28.so
7f79e055e000-7f79e055f000 r--p 00002000 08:02 67117910                   /usr/lib64/libdl-2.28.so
7f79e055f000-7f79e0560000 rw-p 00003000 08:02 67117910                   /usr/lib64/libdl-2.28.so
7f79e0560000-7f79e0589000 r-xp 00000000 08:02 67117821                   /usr/lib64/libtinfo.so.6.1
7f79e0589000-7f79e0788000 ---p 00029000 08:02 67117821                   /usr/lib64/libtinfo.so.6.1
7f79e0788000-7f79e078c000 r--p 00028000 08:02 67117821                   /usr/lib64/libtinfo.so.6.1
7f79e078c000-7f79e078d000 rw-p 0002c000 08:02 67117821                   /usr/lib64/libtinfo.so.6.1
7f79e078d000-7f79e07b9000 r-xp 00000000 08:02 67117901                   /usr/lib64/ld-2.28.so
7f79e0950000-7f79e09a3000 r--p 00000000 08:02 134352640                  /usr/lib/locale/en_US.utf8/LC_CTYPE
7f79e09a3000-7f79e09a4000 r--p 00000000 08:02 134352643                  /usr/lib/locale/en_US.utf8/LC_NUMERIC
7f79e09a4000-7f79e09a5000 r--p 00000000 08:02 201334461                  /usr/lib/locale/en_US.utf8/LC_TIME
7f79e09a5000-7f79e09a6000 r--p 00000000 08:02 201334459                  /usr/lib/locale/en_US.utf8/LC_MONETARY
7f79e09a6000-7f79e09a7000 r--p 00000000 08:02 201334471                  /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
7f79e09a7000-7f79e09a8000 r--p 00000000 08:02 67117882                   /usr/lib/locale/en_US.utf8/LC_PAPER
7f79e09a8000-7f79e09a9000 r--p 00000000 08:02 134352642                  /usr/lib/locale/en_US.utf8/LC_NAME
7f79e09a9000-7f79e09b0000 r--s 00000000 08:02 67152228                   /usr/lib64/gconv/gconv-modules.cache
7f79e09b0000-7f79e09b5000 rw-p 00000000 00:00 0 
7f79e09b5000-7f79e09b6000 r--p 00000000 08:02 201334456                  /usr/lib/locale/en_US.utf8/LC_ADDRESS
7f79e09b6000-7f79e09b7000 r--p 00000000 08:02 201334460                  /usr/lib/locale/en_US.utf8/LC_TELEPHONE
7f79e09b7000-7f79e09b8000 r--p 00000000 08:02 201334458                  /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
7f79e09b8000-7f79e09b9000 r--p 00000000 08:02 201334457                  /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
7f79e09b9000-7f79e09ba000 r--p 0002c000 08:02 67117901                   /usr/lib64/ld-2.28.so
7f79e09ba000-7f79e09bc000 rw-p 0002d000 08:02 67117901                   /usr/lib64/ld-2.28.so
7fffaa035000-7fffaa056000 rw-p 00000000 00:00 0                          [stack]
7fffaa0c3000-7fffaa0c7000 r--p 00000000 00:00 0                          [vvar]
7fffaa0c7000-7fffaa0c9000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

#查看系统调用
[root@rocky8 ~]# strace ls
-bash: strace: command not found
[root@rocky8 ~]# dnf -y install strace

[root@rocky8 ~]# strace ls
execve("/usr/bin/ls", ["ls"], 0x7fff01663320 /* 24 vars */) = 0
brk(NULL)                               = 0x55e74733d000
arch_prctl(0x3001 /* ARCH_??? */, 0x7fff67de07f0) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=13475, ...}) = 0
mmap(NULL, 13475, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8ef000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200z\0\0\0\0\0\0"..., 832) = 832
lseek(3, 157168, SEEK_SET)              = 157168
read(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32) = 32
fstat(3, {st_mode=S_IFREG|0755, st_size=168000, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f930d8ed000
lseek(3, 157168, SEEK_SET)              = 157168
read(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32) = 32
mmap(NULL, 2266608, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f930d49d000
mprotect(0x7f930d4c4000, 2093056, PROT_NONE) = 0
mmap(0x7f930d6c3000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x26000) = 0x7f930d6c3000
mmap(0x7f930d6c5000, 5616, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f930d6c5000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libcap.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\30\0\0\0\0\0\0"..., 832) = 832
lseek(3, 15728, SEEK_SET)               = 15728
read(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\1\0\0\300\4\0\0\0\30\0\0\0\0\0\0\0"..., 48) = 48
fstat(3, {st_mode=S_IFREG|0755, st_size=24984, ...}) = 0
lseek(3, 15728, SEEK_SET)               = 15728
read(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\1\0\0\300\4\0\0\0\30\0\0\0\0\0\0\0"..., 48) = 48
mmap(NULL, 2117944, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f930d297000
mprotect(0x7f930d29b000, 2097152, PROT_NONE) = 0
mmap(0x7f930d49b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x4000) = 0x7f930d49b000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\2605\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=3168120, ...}) = 0
lseek(3, 808, SEEK_SET)                 = 808
read(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\1\0\0\300\4\0\0\0\330\1\0\0\0\0\0\0"..., 48) = 48
mmap(NULL, 3950400, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f930ced2000
mprotect(0x7f930d08e000, 2093056, PROT_NONE) = 0
mmap(0x7f930d28d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7f930d28d000
mmap(0x7f930d293000, 14144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f930d293000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libpcre2-8.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200#\0\0\0\0\0\0"..., 832) = 832
lseek(3, 532976, SEEK_SET)              = 532976
read(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\1\0\0\300\4\0\0\0\30\0\0\0\0\0\0\0"..., 48) = 48
fstat(3, {st_mode=S_IFREG|0755, st_size=543152, ...}) = 0
lseek(3, 532976, SEEK_SET)              = 532976
read(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\1\0\0\300\4\0\0\0\30\0\0\0\0\0\0\0"..., 48) = 48
mmap(NULL, 2634280, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f930cc4e000
mprotect(0x7f930ccd1000, 2093056, PROT_NONE) = 0
mmap(0x7f930ced0000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x82000) = 0x7f930ced0000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\20\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=28968, ...}) = 0
mmap(NULL, 2109744, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f930ca4a000
mprotect(0x7f930ca4d000, 2093056, PROT_NONE) = 0
mmap(0x7f930cc4c000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f930cc4c000
close(3)                                = 0
openat(AT_FDCWD, "/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200n\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=320504, ...}) = 0
mmap(NULL, 2225344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f930c82a000
mprotect(0x7f930c845000, 2093056, PROT_NONE) = 0
mmap(0x7f930ca44000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1a000) = 0x7f930ca44000
mmap(0x7f930ca46000, 13504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f930ca46000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f930d8eb000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f930d8e9000
arch_prctl(ARCH_SET_FS, 0x7f930d8ec640) = 0
mprotect(0x7f930d28d000, 16384, PROT_READ) = 0
mprotect(0x7f930ca44000, 4096, PROT_READ) = 0
mprotect(0x7f930cc4c000, 4096, PROT_READ) = 0
mprotect(0x7f930ced0000, 4096, PROT_READ) = 0
mprotect(0x7f930d49b000, 4096, PROT_READ) = 0
mprotect(0x7f930d6c3000, 4096, PROT_READ) = 0
mprotect(0x55e745d4d000, 8192, PROT_READ) = 0
mprotect(0x7f930d8f3000, 4096, PROT_READ) = 0
munmap(0x7f930d8ef000, 13475)           = 0
set_tid_address(0x7f930d8ec910)         = 15182
set_robust_list(0x7f930d8ec920, 24)     = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7f930c830900, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7f930c83cb30}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7f930c830990, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7f930c83cb30}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
statfs("/sys/fs/selinux", 0x7fff67de07a0) = -1 ENOENT (No such file or directory)
statfs("/selinux", 0x7fff67de07a0)      = -1 ENOENT (No such file or directory)
brk(NULL)                               = 0x55e74733d000
brk(0x55e74735e000)                     = 0x55e74735e000
openat(AT_FDCWD, "/proc/filesystems", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
read(3, "nodev\tsysfs\nnodev\ttmpfs\nnodev\tbd"..., 1024) = 334
read(3, "", 1024)                       = 0
close(3)                                = 0
access("/etc/selinux/config", F_OK)     = 0
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2997, ...}) = 0
read(3, "# Locale name alias data base.\n#"..., 4096) = 2997
read(3, "", 4096)                       = 0
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_IDENTIFICATION", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_IDENTIFICATION", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=368, ...}) = 0
mmap(NULL, 368, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8f2000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib64/gconv/gconv-modules.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26998, ...}) = 0
mmap(NULL, 26998, PROT_READ, MAP_SHARED, 3, 0) = 0x7f930d8e2000
close(3)                                = 0
futex(0x7f930d2929a8, FUTEX_WAKE_PRIVATE, 2147483647) = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_MEASUREMENT", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_MEASUREMENT", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=23, ...}) = 0
mmap(NULL, 23, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8f1000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_TELEPHONE", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_TELEPHONE", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=59, ...}) = 0
mmap(NULL, 59, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8f0000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_ADDRESS", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_ADDRESS", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
mmap(NULL, 167, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8ef000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_NAME", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_NAME", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=77, ...}) = 0
mmap(NULL, 77, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8e1000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_PAPER", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_PAPER", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=34, ...}) = 0
mmap(NULL, 34, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8e0000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_MESSAGES", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_MESSAGES", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=29, ...}) = 0
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=57, ...}) = 0
mmap(NULL, 57, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8df000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_MONETARY", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_MONETARY", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=286, ...}) = 0
mmap(NULL, 286, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8de000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_COLLATE", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_COLLATE", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2586930, ...}) = 0
mmap(NULL, 2586930, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930c5b2000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_TIME", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_TIME", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=3316, ...}) = 0
mmap(NULL, 3316, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8dd000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_NUMERIC", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_NUMERIC", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=54, ...}) = 0
mmap(NULL, 54, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d8dc000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/locale/en_US.UTF-8/LC_CTYPE", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/en_US.utf8/LC_CTYPE", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=337024, ...}) = 0
mmap(NULL, 337024, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f930d889000
close(3)                                = 0
ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(1, TIOCGWINSZ, {ws_row=27, ws_col=143, ws_xpixel=0, ws_ypixel=0}) = 0
openat(AT_FDCWD, ".", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0550, st_size=251, ...}) = 0
getdents64(3, 0x55e747347980 /* 16 entries */, 32768) = 528
getdents64(3, 0x55e747347980 /* 0 entries */, 32768) = 0
close(3)                                = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x2), ...}) = 0
write(1, " anaconda-ks.cfg  'aux |grep bas"..., 58 anaconda-ks.cfg  'aux |grep bashq'   test.pipe   test.sh
) = 58
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

#查看库调用
[root@rocky8 ~]# ltrace ls
-bash: ltrace: command not found
[root@rocky8 ~]# dnf -y install ltrace

[root@rocky8 ~]# ltrace ls
strrchr("ls", '/')                                                                      = nil
setlocale(LC_ALL, "")                                                                   = "en_US.UTF-8"
bindtextdomain("coreutils", "/usr/share/locale")                                        = "/usr/share/locale"
textdomain("coreutils")                                                                 = "coreutils"
__cxa_atexit(0x5641524b4690, 0, 0x5641526c6f80, 0)                                      = 0
isatty(1)                                                                               = 1
getenv("QUOTING_STYLE")                                                                 = nil
getenv("COLUMNS")                                                                       = nil
ioctl(1, 21523, 0x7ffe9041e4b0)                                                         = 0
getenv("TABSIZE")                                                                       = nil
getopt_long(1, 0x7ffe9041e5e8, "abcdfghiklmnopqrstuvw:xABCDFGHI:"..., 0x5641526c7280, -1) = -1
getenv("LS_BLOCK_SIZE")                                                                 = nil
getenv("BLOCK_SIZE")                                                                    = nil
getenv("BLOCKSIZE")                                                                     = nil
getenv("POSIXLY_CORRECT")                                                               = nil
getenv("BLOCK_SIZE")                                                                    = nil
__errno_location()                                                                      = 0x7fe40a2454d8
malloc(56)                                                                              = 0x5641538c39c0
memcpy(0x5641538c39c0, "\003\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 56) = 0x5641538c39c0
__errno_location()                                                                      = 0x7fe40a2454d8
malloc(56)                                                                              = 0x5641538c3a00
memcpy(0x5641538c3a00, "\003\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 56) = 0x5641538c3a00
getenv("TZ")                                                                            = nil
malloc(128)                                                                             = 0x5641538c3a40
malloc(20000)                                                                           = 0x5641538c3ad0
malloc(32)                                                                              = 0x5641538c8900
strlen(".")                                                                             = 1
malloc(2)                                                                               = 0x5641538c8930
memcpy(0x5641538c8930, ".\0", 2)                                                        = 0x5641538c8930
__errno_location()                                                                      = 0x7fe40a2454d8
opendir(".")                                                                            = { 3 }
readdir({ 3 })                                                                          = { 201326721, "." }
readdir({ 3 })                                                                          = { 128, ".." }
readdir({ 3 })                                                                          = { 201921796, ".bash_logout" }
readdir({ 3 })                                                                          = { 201921797, ".bash_profile" }
readdir({ 3 })                                                                          = { 201921798, ".bashrc" }
readdir({ 3 })                                                                          = { 201921799, ".cshrc" }
readdir({ 3 })                                                                          = { 201921800, ".tcshrc" }
readdir({ 3 })                                                                          = { 201326724, "anaconda-ks.cfg" }
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
strlen("anaconda-ks.cfg")                                                               = 15
strlen("anaconda-ks.cfg")                                                               = 15
malloc(16)                                                                              = 0x5641538d0990
memcpy(0x5641538d0990, "anaconda-ks.cfg\0", 16)                                         = 0x5641538d0990
readdir({ 3 })                                                                          = { 201929311, ".vimrc" }
readdir({ 3 })                                                                          = { 201929315, ".bash_history" }
readdir({ 3 })                                                                          = { 201327308, "test.pipe" }
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
strlen("test.pipe")                                                                     = 9
strlen("test.pipe")                                                                     = 9
malloc(10)                                                                              = 0x5641538d09b0
memcpy(0x5641538d09b0, "test.pipe\0", 10)                                               = 0x5641538d09b0
readdir({ 3 })                                                                          = { 67644000, ".config" }
readdir({ 3 })                                                                          = { 201328537, "aux |grep bashq" }
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
strlen("aux |grep bashq")                                                               = 15
malloc(16)                                                                              = 0x5641538d09d0
memcpy(0x5641538d09d0, "aux |grep bashq\0", 16)                                         = 0x5641538d09d0
readdir({ 3 })                                                                          = { 201328540, ".lesshst" }
readdir({ 3 })                                                                          = { 201328542, "test.sh" }
strlen("test.sh")                                                                       = 7
malloc(8)                                                                               = 0x5641538d09f0
memcpy(0x5641538d09f0, "test.sh\0", 8)                                                  = 0x5641538d09f0
readdir({ 3 })                                                                          = { 201328543, ".viminfo" }
readdir({ 3 })                                                                          = nil
closedir({ 3 })                                                                         = 0
free(nil)                                                                               = <void>
malloc(96)                                                                              = 0x5641538c8950
_setjmp(0x5641526c82a0, 0x400000, 0x5641538c3df0, 0x5641538c8970)                       = 0
__errno_location()                                                                      = 0x7fe40a2454d8
strcoll("aux |grep bashq", "test.sh")                                                   = -116
__errno_location()                                                                      = 0x7fe40a2454d8
strcoll("anaconda-ks.cfg", "test.pipe")                                                 = -116
memcpy(0x5641538c8978, "\230;\214SAV\0\0", 8)                                           = 0x5641538c8978
__errno_location()                                                                      = 0x7fe40a2454d8
strcoll("anaconda-ks.cfg", "aux |grep bashq")                                           = -2
__errno_location()                                                                      = 0x7fe40a2454d8
strcoll("test.pipe", "aux |grep bashq")                                                 = 116
__errno_location()                                                                      = 0x7fe40a2454d8
strcoll("test.pipe", "test.sh")                                                         = -1
realloc(nil, 192)                                                                       = 0x5641538c89c0
malloc(288)                                                                             = 0x5641538c8a90
strlen("anaconda-ks.cfg")                                                               = 15
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
strlen("test.pipe")                                                                     = 9
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
strlen("test.sh")                                                                       = 7
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
strlen("anaconda-ks.cfg")                                                               = 15
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
strlen("anaconda-ks.cfg")                                                               = 15
__overflow(0x7fe409beb6e0, 32, 0x5641538d0990, 16)                                      = 32
fwrite_unlocked("anaconda-ks.cfg", 1, 15, 0x7fe409beb6e0)                               = 15
__overflow(0x7fe409beb6e0, 32, 1, 8)                                                    = 32
__overflow(0x7fe409beb6e0, 32, 2, 8)                                                    = 32
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
fwrite_unlocked("'aux |grep bashq'", 1, 17, 0x7fe409beb6e0)                             = 17
__overflow(0x7fe409beb6e0, 32, 4, 8)                                                    = 32
__overflow(0x7fe409beb6e0, 32, 5, 8)                                                    = 32
strlen("test.pipe")                                                                     = 9
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
strlen("test.pipe")                                                                     = 9
__overflow(0x7fe409beb6e0, 32, 0x5641538d09b0, 16)                                      = 32
fwrite_unlocked("test.pipe", 1, 9, 0x7fe409beb6e0)                                      = 9
__overflow(0x7fe409beb6e0, 32, 0, 8)                                                    = 32
__overflow(0x7fe409beb6e0, 32, 1, 8)                                                    = 32
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
strlen("test.sh")                                                                       = 7
__ctype_get_mb_cur_max()                                                                = 6
__ctype_get_mb_cur_max()                                                                = 6
__errno_location()                                                                      = 0x7fe40a2454d8
__ctype_get_mb_cur_max()                                                                = 6
strlen("test.sh")                                                                       = 7
__overflow(0x7fe409beb6e0, 32, 0x5641538d09f0, 16)                                      = 32
fwrite_unlocked("test.sh", 1, 7, 0x7fe409beb6e0)                                        = 7
__overflow(0x7fe409beb6e0, 10, 7, 0 anaconda-ks.cfg  'aux |grep bashq'   test.pipe   test.sh
)                                                    = 10
free(0x5641538c8930)                                                                    = <void>
free(nil)                                                                               = <void>
free(0x5641538c8900)                                                                    = <void>
__fpending(0x7fe409beb6e0, 0, 0x5641524b4690, 1)                                        = 0
fileno(0x7fe409beb6e0)                                                                  = 1
__freading(0x7fe409beb6e0, 0, 0x5641524b4690, 1)                                        = 0
__freading(0x7fe409beb6e0, 0, 2052, 1)                                                  = 0
fflush(0x7fe409beb6e0)                                                                  = 0
fclose(0x7fe409beb6e0)                                                                  = 0
__fpending(0x7fe409beb600, 0, 0x7fe409be6880, 2880)                                     = 0
fileno(0x7fe409beb600)                                                                  = 2
__freading(0x7fe409beb600, 0, 0x7fe409be6880, 2880)                                     = 0
__freading(0x7fe409beb600, 0, 4, 2880)                                                  = 0
fflush(0x7fe409beb600)                                                                  = 0
fclose(0x7fe409beb600)                                                                  = 0
__cxa_finalize(0x5641526c6f80, 0x5641526c6f78, 1, 1)                                    = 0x7fe409bec8d8
+++ exited (status 0) +++

2.11 虚拟内存信息 vmstat

格式:

vmstat [options] [delay [count]]

显示项说明:

procs:
    r:可运行(正运行或等待运行)进程的个数,和核心数有关
    b:处于不可中断睡眠态的进程个数(被阻塞的队列的长度)
memory:
    swpd: 交换内存的使用总量
    free:空闲物理内存总量
    buffer:用于buffer的内存总量
    cache:用于cache的内存总量
swap:
    si:从磁盘交换进内存的数据速率(kb/s)
    so:从内存交换至磁盘的数据速率(kb/s)
io:
    bi:从块设备读入数据到系统的速率(kb/s)
    bo: 保存数据至块设备的速率
system:
    in: interrupts 中断速率,包括时钟
    cs: context switch 进程切换速率
cpu:
    us:Time spent running non-kernel code
    sy: Time spent running kernel code
    id: Time spent idle. Linux 2.5.41前,包括IO-wait time.
    wa: Time spent waiting for IO. 2.5.41前,包括in idle.
    st: Time stolen from a virtual machine. 2.6.11前, unknown.

选项:

-s: 显示内存的统计数据

范例:

[root@rocky8 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 161444 144140      0 204936    1    6    36    46  394  156  6 23 71  0  0

[root@rocky8 ~]# vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 161444 143636      0 204976    1    6    36    45  393  156  6 23 71  0  0
 0  0 161444 143384      0 204976    0    0     0     0  173  271  0  0 100  0  0
 0  0 161444 143384      0 204976    0    0     0     0  169  265  0  1 100  0  0

[root@rocky8 ~]# dd if=/dev/zero of=/dev/null bs=2G count=1
0+1 records in
0+1 records out
2147479552 bytes (2.1 GB, 2.0 GiB) copied, 4.86451 s, 441 MB/s

#so  增长,swqp output 因为它是以memory 为参照物,它指的内存的出

[root@rocky8 ~]# vmstat 1
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 426476 607980      0  52244    1   70    37   109  395  156  6 23 71  0  0
...
 2  0 835492  44236      0  33704 1016 409848  3584 409897 23147 1293  0 76 24  0  0
 2  0 1241576  45968      0  33896  592 406556  1732 406556 27253  750  0 84 16  0  0
 2  0 1644304  42404      0  32484  468 403416   468 403416 27321  693  0 82 18  0  0
 0  0 430124 630036      0  33852  684 340164  3940 340164 17841  994  0 64 36  0  0


[root@rocky8 ~]# dd if=/dev/sda of=/dev/null
^C31061791+0 records in
31061790+0 records out
15903636480 bytes (16 GB, 15 GiB) copied, 15.417 s, 1.0 GB/s

#bi  增长,它以内存为参照物


[root@rocky8 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 429100 608712      0  53568    1  124    38   163  397  156  6 22 71  0  0
 0  0 429100 608712      0  53628   12    0    12     0  170  278  0  0 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  152  234  0  0 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  155  256  0  1 99  0  0
 0  0 429100 608712      0  53628    0    0     0     0  146  256  0  0 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  167  266  0  1 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  173  271  0  0 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  150  256  0  0 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  157  256  0  1 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  177  280  0  0 100  0  0
 0  0 429100 608712      0  53628    0    0     0     0  165  255  0  1 99  0  0
 1  0 429100 580228  28496  53624   52    0 28548     0  352  351  0  3 97  0  0
 1  0 429100  80020 537308  44084    0    0 995328     0 3202  686  5 65 30  0  0
 1  0 429100  63424 556916  41004    0    0 892928     0 2901  474  6 55 39  0  0
 2  0 429100  48728 568692  41712    0    0 1007616     0 2841  491  7 54 39  0  0
 1  0 429100  62404 555248  43736    0    0 1032208     0 2944  521  8 55 38  0  0
 1  0 429100  51920 563480  46140    0    0 1015808     0 2953  563  6 56 38  0  0
 1  0 429100  51112 563832  46468    0    0 1036288     0 3214  717  8 56 37  0  0
 1  0 429100  59728 555264  46528    0    0 1019904     0 3272  782  8 54 38  0  0
 1  0 429100  52792 561940  46688    0    0 1007616     0 3254  772  6 57 37  0  0
 2  0 429100  54960 559708  46780    0    0 1032212     0 3250  735  7 56 37  0  0
 1  0 429100  54464 560468  46456    0    0 1015808     0 3255  764  7 56 38  0  0
 2  0 429100  58452 553540  46440    0    0 991232     0 3242  739  6 56 38  0  0
 3  0 429100  66520 548476  46544    0    0 1007616     0 3256  760  7 56 38  0  0
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 429100  55688 558988  46664    0    0 1019904     0 3320  776  8 55 37  0  0
 2  0 429100  61720 552588  46340    0    0 1019904     0 3292  786  7 56 37  0  0
 1  0 429100  67816 545968  46516    0    0 1036288     0 3350  802  6 57 37  0  0
 0  0 429100  64656 550028  46416    0    0 376832     0 1318  458  2 21 77  0  0

[root@rocky8 ~]# dd if=/dev/zero of=f1.img
^C7872812+0 records in
7872812+0 records out
4030879744 bytes (4.0 GB, 3.8 GiB) copied, 6.52304 s, 618 MB/s

#bo 增长  

[root@rocky8 ~]# vmstat 1
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 429100  66380      0 595296    1  123   575   488  398  157  6 22 71  0  0
 0  0 429100  66380      0 595344    0    0     0     0  140  240  0  0 100  0  0
 0  0 429100  66380      0 595344    0    0     0     0  143  254  0  0 100  0  0
 0  0 429100  66380      0 595344    0    0     0 46931  271  274  0  3 97  0  0
 0  0 429100  66380      0 595344   12    0    12     0  167  276  0  0 100  0  0
 5  0 429100 148272      0 514036    0    0    16 423938 1812  385  5 54 41  0  0
 1  0 429100  77728      0 584940    0    0    16 600099 2294  446  6 72 23  0  0
 1  0 429100  65104      0 596288    0    0     0 593922 2351  490  6 71 23  0  0
 1  0 429100  51764      0 609512    0    0     0 604076 2317  442  6 72 22  0  0
 2  0 429100  52244      0 609060    0    0     0 614401 2408  494  6 74 20  0  0
 2  0 429100  78452      0 582860    0    0     0 593920 2349  401  7 72 21  0  0
 0  0 429100  64028      0 597024    0    0     0 450562 1941  427  4 56 40  0  0

范例:

[root@rocky8 ~]# vmstat -s
       801164 K total memory
       139904 K used memory
        12636 K active memory
       560876 K inactive memory
        61304 K free memory
            0 K buffer memory
       599956 K swap cache
      2097148 K total swap
       429100 K used swap
      1668048 K free swap
       176209 non-nice user cpu ticks
          305 nice user cpu ticks
       616742 system cpu ticks
      2077191 idle cpu ticks
          243 IO-wait cpu ticks
         7232 IRQ cpu ticks
        24903 softirq cpu ticks
            0 stolen cpu ticks
     16632392 pages paged in
     18105063 pages paged out
         6432 pages swapped in
       891099 pages swapped out
     11526267 interrupts
      4544888 CPU context switches
   1635656624 boot time
        15629 forks

2.12 统计CPU和设备IO信息 iostat

此工具由sysstat包提供

范例:

[root@rocky8 ~]# iostat
Linux 4.18.0-305.3.1.el8_4.x86_64 (rocky8) 	10/31/2021 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           6.05    0.01   22.29    0.01    0.00   71.64

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.06         0.00       1041          0
sda               4.90       955.22      1039.76   16633034   18105087

[root@rocky8 ~]# iostat 1
Linux 4.18.0-305.3.1.el8_4.x86_64 (rocky8) 	10/31/2021 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           6.04    0.01   22.25    0.01    0.00   71.69

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.06         0.00       1041          0
sda               4.89       953.73      1038.14   16633046   18105090

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.50    0.00    0.00   99.50

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda               0.00         0.00         0.00          0          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.50    0.00    0.00   99.50

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda               0.00         0.00         0.00          0          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda               1.00         4.00         0.00          4          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.50    0.00    0.00   99.50

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda               0.00         0.00         0.00          0          0

^C

[root@rocky8 ~]# iostat /dev/sda
Linux 4.18.0-305.3.1.el8_4.x86_64 (rocky8) 	10/31/2021 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           6.03    0.01   22.20    0.01    0.00   71.76

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               4.88       951.90      1036.14   16633050   18105155

[root@rocky8 ~]# iostat /dev/sda |awk /sda/'{print $2.$3.$4}'
4.88951.061035.23
[root@rocky8 ~]# iostat /dev/sda |grep sda|tr -s ' '|cut -d" " -f2-7
4.88 950.86 1034.74 16637426 18105157

2.13 系统资源统计 dstat

dstat由pcp-system-tools包提供,用于代替 vmstat,iostat功能

格式:

dstat [-afv] [options..] [delay [count]]

常用选项

-c 显示cpu相关信息
-C #,#,...,total
-d 显示disk相关信息
-D total,sda,sdb,...
-g 显示page相关统计数据
-m 显示memory相关统计数据
-n 显示network相关统计数据
-p 显示process相关统计数据
-r 显示io请求相关的统计数据
-s 显示swapped相关的统计数据
--tcp
--udp
--unix
--raw
--socket
--ipc
--top-cpu:显示最占用CPU的进程
--top-io: 显示最占用io的进程
--top-mem: 显示最占用内存的进程
--top-latency: 显示延迟最大的进程

范例:

[root@rocky8 ~]# dstat
-bash: dstat: command not found
[root@rocky8 ~]# dnf -y install dstat

[root@rocky8 ~]# dstat
You did not select any stats, using -cdngy by default.
----total-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai stl| read  writ| recv  send|  in   out | int   csw 
  1   0 100   0   0|   0     0 |  60B  617B|   0     0 | 199   327 
  0   0 100   0   0|   0     0 |  60B  314B|   0     0 | 190   317 
  0   0  99   0   0|   0     0 |  60B  314B|   0     0 | 182   303 
  0   0  99   0   0|   0     0 |  60B  314B|   0     0 | 188   314 
  0   0  99   0   0|   0     0 |  60B  314B|   0     0 | 187   319 
  0   0 100   0   0|   0     0 |  60B  314B|   0     0 | 200   330 
  0   0  99   0   0|   0     0 |  60B  314B|   0     0 | 179   306 
  1   0  99   0   0|   0     0 |  60B  314B|   0     0 | 181   298 
  0   0  99   0   0|   0     0 | 166B  390B|   0     0 | 207   329 
  0   0 100   0   0|   0     0 |  60B  314B|   0     0 | 200   323 
  0   0  99   0   0|   0     0 |  60B  314B|   0     0 | 186   316 
  1   1  99   0   0|   0  1504k|  60B  314B|   0     0 | 215   333 ^C

[root@rocky8 ~]# dstat 1 3
You did not select any stats, using -cdngy by default.
----total-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai stl| read  writ| recv  send|  in   out | int   csw 
  0   0  99   0   0|   0     0 |  60B  617B|   0     0 | 196   319 
  0   0 100   0   0|   0     0 |  60B  314B|   0     0 | 199   322 
  0   0  99   0   0|   0     0 |  60B  314B|   0     0 | 194   314 
  
[root@rocky8 ~]# dd if=/dev/sda of=/dev/null

[root@centos7 ~]# ping -f -s 65507 172.31.1.8
PING 172.31.1.8 (172.31.1.8) 65507(65535) bytes of data.

[root@rocky8 ~]# dstat
You did not select any stats, using -cdngy by default.
----total-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai stl| read  writ| recv  send|  in   out | int   csw 
 23  50  26   0   0| 910M    0 |  17M   17M|   0     0 |8224   977 
 24  49  27   0   0| 921M    0 |  17M   17M|   0     0 |8147   956 
 25  48  27   0   0| 912M    0 |  16M   16M|   0     0 |8054   895 
 24  49  26   0   0| 904M    0 |  17M   16M|   0     0 |8222   958 
 24  49  27   0   0| 908M 2049B|  17M   17M|   0     0 |8336   939 
 24  49  26   0   0| 892M    0 |  17M   17M|   0     0 |8249   931 
 24  48  27   0   0| 911M    0 |  16M   16M|   0     0 |8068   907 
 24  49  27   0   0| 887M    0 |  17M   17M|   0     0 |8283   895 
 24  49  28   0   0| 893M    0 |  17M   17M|   0     0 |8257   934 
 24  48  27   0   0| 872M    0 |  17M   17M|   0     0 |8110   913 
 24  49  26   0   0| 876M    0 |  16M   16M|   0     0 |7965   922 
 25  49  27   0   0| 892M    0 |  17M   17M|   0     0 |8122   942 
 22  49  26   0   0| 883M    0 |  17M   17M|   0     0 |7967   960 
 24  49  27   0   0| 917M    0 |  16M   16M|   0     0 |8254   962 ^C

2.14 监视磁盘I/O iotop

来自于iotop包

[root@rocky8 ~]# iotop
-bash: iotop: command not found
[root@rocky8 ~]# dnf -y install iotop

在这里插入图片描述
iotop命令是一个用来监视磁盘I/O使用状况的top类工具iotop具有与top相似的UI,其中包括PID、用户、I/O、进程等相关信息,可查看每个进程是如何使用IO

iotop输出

线程ID(按p切换为进程ID)
优先级
用户
磁盘读速率
磁盘写速率
swap交换百分比
IO等待所占的百分比

iotop常用参数

-o, --only只显示正在产生I/O的进程或线程,除了传参,可以在运行过程中按o生效
-b, --batch非交互模式,一般用来记录日志
-n NUM, --iter=NUM设置监测的次数,默认无限。在非交互模式下很有用
-d SEC, --delay=SEC设置每次监测的间隔,默认1秒,接受非整形数据例如1.1
-p PID, --pid=PID指定监测的进程/线程
-u USER, --user=USER指定监测某个用户产生的I/O
-P, --processes仅显示进程,默认iotop显示所有线程
-a, --accumulated显示累积的I/O,而不是带宽
-k, --kilobytes使用kB单位,而不是对人友好的单位。在非交互模式下,脚本编程有用
-t, --time 加上时间戳,非交互非模式
-q, --quiet 禁止头几行,非交互模式,有三种指定方式
-q 只在第一次监测时显示列名
-qq 永远不显示列名
-qqq 永远不显示I/O汇总

交互按键

left和right方向键:改变排序
r:反向排序
o:切换至选项--only
p:切换至--processes选项
a:切换至--accumulated选项
q:退出
i:改变线程的优先级

范例:

[root@rocky8 ~]# iotop
Total DISK READ :	0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:	0.00 B/s | Actual DISK WRITE:       0.00 B/s
    TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND                                                                       
      1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 17
      2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
      3 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_gp]
      4 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_par_gp]
      6 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H-events_highpri]
      9 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [mm_percpu_wq]
     10 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
     11 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
     12 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     13 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
     14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [cpuhp/0]
     16 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
     17 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]
     18 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kauditd]
     19 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khungtaskd]
     20 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [oom_reaper]
     21 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [writeback]
     22 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kcompactd0]
     23 be/5 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksmd]
     24 be/7 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khugepaged]
     25 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [crypto]
     26 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kintegrityd]
     27 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kblockd]

[root@rocky8 ~]#  dd if=/dev/sda of=/dev/null

[root@rocky8 ~]# iotop
Total DISK READ :     907.52 M/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:     907.52 M/s | Actual DISK WRITE:       0.00 B/s
    TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND                                                                       
  16132 be/4 root      907.52 M/s    0.00 B/s  0.00 %  0.00 % dd if=/dev/sda of=/dev/null
      1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 17
      2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
      3 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_gp]
      4 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_par_gp]
      6 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H-events_highpri]
      9 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [mm_percpu_wq]
     10 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
     11 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
     12 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     13 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
     14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [cpuhp/0]
     16 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
     17 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]
     18 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kauditd]
     19 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khungtaskd]
     20 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [oom_reaper]
     21 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [writeback]
     22 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kcompactd0]
     23 be/5 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksmd]
     24 be/7 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khugepaged]
     25 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [crypto]
     26 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kintegrityd]

[root@rocky8 ~]# dd if=/dev/zero of=f1.img

[root@rocky8 ~]# iotop -n 1 |head -1
Total DISK READ :	0.00 B/s | Total DISK WRITE :     480.76 M/s

2.15 显示网络带宽使用情况 iftop

通过EPEL源安装iftop包

[root@rocky8 ~]# iftop
-bash: iftop: command not found
[root@rocky8 ~]# dnf -y install iftop

[root@rocky8 ~]# iftop

在这里插入图片描述
范例:

[root@centos7 ~]# ping -f -s 65507 172.31.1.8
PING 172.31.1.8 (172.31.1.8) 65507(65535) bytes of data.

[root@rocky8 ~]# iftop
interface: eth0
IP address is: 172.31.1.8
MAC address is: 00:0c:29:f9:6a:d1
                            1.86Gb                       3.73Gb                      5.59Gb                       7.45Gb		 9.31Gb
└───────────────────────────┴────────────────────────────┴───────────────────────────┴────────────────────────────┴────────────────────────────
rocky8                                                    => 172.31.0.7                                                 85.8Mb  85.8Mb  85.8Mb
                                                          <=                                                            85.9Mb  85.9Mb  85.9Mb
rocky8                                                    => 172.31.0.1                                                 8.62Kb  8.62Kb  8.62Kb
                                                          <=                                                             920b    920b    920b
rocky8                                                    => public1.alidns.com                                         1.07Kb  1.07Kb  1.07Kb
                                                          <=                                                            2.10Kb  2.10Kb  2.10Kb















───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
TX:             cum:   36.6MB   peak:	85.8Mb                                                                 rates:	85.8Mb  85.8Mb  85.8Mb
RX:                    36.7MB           85.9Mb                                                                          85.9Mb  85.9Mb  85.9Mb
[root@rocky8 ~]#       73.3MB            172Mb                                                                           172Mb   172Mb   172Mb


[root@rocky8 ~]# iftop -n #-n  纯数字显示
interface: eth0
IP address is: 172.31.1.8
MAC address is: 00:0c:29:f9:6a:d1
                            18.6Gb                       37.3Gb                      55.9Gb                       74.5Gb		 93.1Gb
└───────────────────────────┴────────────────────────────┴───────────────────────────┴────────────────────────────┴────────────────────────────
172.31.1.8                                                => 172.31.0.7                                                  122Mb   122Mb   122Mb
                                                          <=                                                             121Mb   123Mb   123Mb
172.31.1.8                                                => 172.31.0.1                                                 3.09Kb  4.25Kb  4.25Kb
                                                          <=                                                             368b    368b    368b

















───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
TX:             cum:   61.3MB   peak:	 123Mb                                                                 rates:	 122Mb   123Mb   123Mb
RX:                    61.3MB            124Mb                                                                           121Mb   123Mb   123Mb
[root@rocky8 ~]#        123MB            247Mb                                                                           243Mb   245Mb   245Mb

2.16 查看网络实时吞吐量 nload

[root@rocky8 ~]# nload
-bash: nload: command not found
[root@rocky8 ~]# dnf -y install nload

[root@rocky8 ~]# nload

在这里插入图片描述
nload 是一个实时监控网络流量和带宽使用情况,以数值和动态图展示进出的流量情况,通过EPEL源安装
界面操作

上下方向键、左右方向键、enter键或者tab键都就可以切换查看多个网卡的流量情况
按 F2 显示选项窗口
按 q 或者 Ctrl+C 退出 nload

范例:

#默认只查看第一个网络的流量进出情况
[root@rocky8 ~]# nload


Device eth0 [172.31.1.8] (1/2):
===============================================================================================================================================
Incoming:
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #  Curr: 127.88 MBit/s
                                                                                              #  Avg: 127.88 MBit/s
                                                                                              #  Min: 127.88 MBit/s
                                                                                              #  Max: 127.88 MBit/s
                                                                                              #  Ttl: 24.98 GByte
Outgoing:
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #
                                                                                              #  Curr: 127.89 MBit/s
                                                                                              #  Avg: 127.89 MBit/s
                                                                                              #  Min: 127.89 MBit/s
                                                                                              #  Max: 127.89 MBit/s
                                                                                              #  Ttl: 25.64 GByte
...

#在nload后面指定网卡,可以指定多个,按左右键分别显示网卡状态
nload eth0 eth1

#设置刷新间隔:默认刷新间隔是100毫秒,可通过 -t 命令设置刷新时间(单位是毫秒)
nload -t 500 eth0

#设置单位:显示两种单位一种是显示Bit/s、一种是显示Byte/s,默认是以Bit/s,也可不显示/s
#-u h|b|k|m|g|H|B|K|M|G 表示的含义: h: auto, b: Bit/s, k: kBit/s, m: MBit/s, H:auto, B: Byte/s, K: kByte/s, M: MByte/s
nload -u M eth0

2.17 综合监控工具 glances

[root@rocky8 ~]# glances
-bash: glances: command not found
[root@rocky8 ~]# dnf -y install glances
[root@rocky8 ~]# glances

在这里插入图片描述
此工具可以通过EPEL源安装,CentOS 8 目前已提供,但测试有问题

格式:

glances [-bdehmnrsvyz1] [-B bind] [-c server] [-C conffile] [-p port] [-P password] [--password] [-t refresh] [-f file] [-o output]

内建命令:

a Sort processes automatically     l Show/hide logs
c Sort processes by CPU%           b Bytes or bits for network I/O
m Sort processes by MEM%           w Delete warning logs
p Sort processes by name           x Delete warning and critical logs
i Sort processes by I/O rate       1 Global CPU or per-CPU stats
d Show/hide disk I/O stats         h Show/hide this help screen
f Show/hide file system stats      t View network I/O as combination
n Show/hide network stats          u View cumulative network I/O
s Show/hide sensors stats          q Quit (Esc and Ctrl-C also work)
y Show/hide hddtemp stats

常用选项:

-b: 以Byte为单位显示网卡数据速率
-d: 关闭磁盘I/O模块
-f /path/to/somefile: 设定输入文件位置
-o {HTML|CSV}:输出格式
-m: 禁用mount模块
-n: 禁用网络模块
-t #: 延迟时间间隔
-1:每个CPU的相关数据单独显示

C/S模式下运行glances命令

glances -s -B IPADDR
IPADDR: 指明监听的本机哪个地址
glances -c IPADDR
IPADDR:要连入的服务器端地址

范例:

root@ubuntu1804:~# glances -s
Glances XML-RPC server is running on 0.0.0.0:61209

root@ubuntu1804:~# ss -ntl
State             Recv-Q             Send-Q                          Local Address:Port                          Peer Address:Port             
LISTEN            0                  128                             127.0.0.53%lo:53                                 0.0.0.0:*                
LISTEN            0                  128                                   0.0.0.0:22                                 0.0.0.0:*                
LISTEN            0                  5                                     0.0.0.0:61209                              0.0.0.0:*                
LISTEN            0                  128                                 127.0.0.1:6010                               0.0.0.0:*                
LISTEN            0                  128                                 127.0.0.1:6011                               0.0.0.0:*                
LISTEN            0                  128                                      [::]:22                                    [::]:*                
LISTEN            0                  128                                     [::1]:6010                                  [::]:*                
LISTEN            0                  128                                     [::1]:6011                                  [::]:* 

root@ubuntu1804-2:~# glances -c 172.31.2.18

在这里插入图片描述

2.18 查看进程打开文件 lsof

lsof:list open files,查看当前系统文件的工具。在linux环境下,一切皆文件,用户通过文件不仅可以访问常规数据,还可以访问网络连接和硬件如传输控制协议 (TCP) 和用户数据报协议 (UDP)套接字等,系统在后台都为该应用程序分配了一个文件描述符

命令选项:

-a:列出打开文件存在的进程
-c<进程名>:列出指定进程所打开的文件
-g:列出GID号进程详情
-d<文件号>:列出占用该文件号的进程
+d<目录>:列出目录下被打开的文件
+D<目录>:递归列出目录下被打开的文件
-n<目录>:列出使用NFS的文件
-i<条件>:列出符合条件的进程(4、6、协议、:端口、 @ip )
-p<进程号>:列出指定进程号所打开的文件
-u:列出UID号进程详情
-h:显示帮助信息
-v:显示版本信息。
-n: 不反向解析网络名字

范例:

[root@rocky8 ~]# lsof
-bash: lsof: command not found
[root@rocky8 ~]# dnf -y install lsof

[root@rocky8 ~]# lsof |less
COMMAND     PID   TID TASKCMD      USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
systemd       1                    root  cwd       DIR                8,2      256        128 /
systemd       1                    root  rtd       DIR                8,2      256        128 /
systemd       1                    root  txt       REG                8,2  1589040     484070 /usr/lib/systemd/systemd
systemd       1                    root  mem       REG                8,2   147360   67167846 /usr/lib64/libnl-3.so.200.26.0
systemd       1                    root  mem       REG                8,2   549872   67167854 /usr/lib64/libnl-route-3.so.200.26.0
systemd       1                    root  mem       REG                8,2   131144   67543778 /usr/lib64/libibverbs.so.1.11.32.0
systemd       1                    root  mem       REG                8,2  2191808   67117912 /usr/lib64/libm-2.28.so
systemd       1                    root  mem       REG                8,2   633032   67556008 /usr/lib64/libudev.so.1.6.11
systemd       1                    root  mem       REG                8,2   739264   67152232 /usr/lib64/libsepol.so.1
systemd       1                    root  mem       REG                8,2  1580488   67167863 /usr/lib64/libunistring.so.2.1.0
systemd       1                    root  mem       REG                8,2   312496   67543796 /usr/lib64/libpcap.so.1.9.1
systemd       1                    root  mem       REG                8,2   138792   67152254 /usr/lib64/libgpg-error.so.0.24.2
systemd       1                    root  mem       REG                8,2    66744   67167805 /usr/lib64/libjson-c.so.4.0.0
systemd       1                    root  mem       REG                8,2   371208   67582024 /usr/lib64/libdevmapper.so.1.02
systemd       1                    root  mem       REG                8,2    24784   67167807 /usr/lib64/libattr.so.1.1.2448
systemd       1                    root  mem       REG                8,2  3071432   67582048 /usr/lib64/libcrypto.so.1.1.1g
systemd       1                    root  mem       REG                8,2   615576   67582050 /usr/lib64/libssl.so.1.1.1g
systemd       1                    root  mem       REG                8,2    95456   67152235 /usr/lib64/libz.so.1.2.11
systemd       1                    root  mem       REG                8,2    25112   67167753 /usr/lib64/libcap-ng.so.0.0.0
systemd       1                    root  mem       REG                8,2    33384   67152345 /usr/lib64/libuuid.so.1.3.0
systemd       1                    root  mem       REG                8,2    28968   67117910 /usr/lib64/libdl-2.28.so
systemd       1                    root  mem       REG                8,2   543152   67117790 /usr/lib64/libpcre2-8.so.0.7.1
systemd       1                    root  mem       REG                8,2   343608   67556010 /usr/lib64/libblkid.so.1.1.0
systemd       1                    root  mem       REG                8,2   119672   67188491 /usr/lib64/liblz4.so.1.8.3
systemd       1                    root  mem       REG                8,2   162224   67152239 /usr/lib64/liblzma.so.5.2.4

[root@rocky8 ~]# lsof -i :22
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd      765 root    4u  IPv4  26729      0t0  TCP *:ssh (LISTEN)
sshd      765 root    6u  IPv6  26740      0t0  TCP *:ssh (LISTEN)
sshd    10186 root    5u  IPv4  72386      0t0  TCP rocky8:ssh->172.31.0.1:58670 (ESTABLISHED)
sshd    10188 root    5u  IPv4  72386      0t0  TCP rocky8:ssh->172.31.0.1:58670 (ESTABLISHED)
sshd    10449 root    5u  IPv4  73393      0t0  TCP rocky8:ssh->172.31.0.1:58954 (ESTABLISHED)
sshd    10451 root    5u  IPv4  73393      0t0  TCP rocky8:ssh->172.31.0.1:58954 (ESTABLISHED)
sshd    10481 root    5u  IPv4  73590      0t0  TCP rocky8:ssh->172.31.0.1:58955 (ESTABLISHED)
sshd    10483 root    5u  IPv4  73590      0t0  TCP rocky8:ssh->172.31.0.1:58955 (ESTABLISHED)

[root@rocky8 ~]# tail -f /var/log/messages 
Oct 31 18:29:24 rocky8 systemd[1]: Starting man-db-cache-update.service...
Oct 31 18:29:24 rocky8 systemd[1]: man-db-cache-update.service: Succeeded.
Oct 31 18:29:24 rocky8 systemd[1]: Started man-db-cache-update.service.
Oct 31 18:29:24 rocky8 systemd[1]: run-rf03d282b475c4d3bac2b755905b8b453.service: Succeeded.
Oct 31 18:48:07 rocky8 kernel: perf: interrupt took too long (6615 > 6601), lowering kernel.perf_event_max_sample_rate to 30000
Oct 31 18:58:00 rocky8 systemd[1]: Started /usr/bin/systemctl start man-db-cache-update.
Oct 31 18:58:00 rocky8 systemd[1]: Starting man-db-cache-update.service...
Oct 31 18:58:00 rocky8 systemd[1]: man-db-cache-update.service: Succeeded.
Oct 31 18:58:00 rocky8 systemd[1]: Started man-db-cache-update.service.
Oct 31 18:58:00 rocky8 systemd[1]: run-re58cc3ca0a874565bb0b1cf41e3cb739.service: Succeeded.

[root@rocky8 ~]# pstree -p |grep tail
           |-sshd(765)-+-sshd(10186)---sshd(10188)---bash(10189)---tail(17292)
           
[root@rocky8 ~]# lsof -p 17292
COMMAND   PID USER   FD      TYPE DEVICE SIZE/OFF      NODE NAME
tail    17292 root  cwd       DIR    8,2      251 201326721 /root
tail    17292 root  rtd       DIR    8,2      256       128 /
tail    17292 root  txt       REG    8,2    76120    423833 /usr/bin/tail
tail    17292 root  mem       REG    8,2  2586930 134352575 /usr/lib/locale/en_US.utf8/LC_COLLATE
tail    17292 root  mem       REG    8,2  3168120  67117908 /usr/lib64/libc-2.28.so
tail    17292 root  mem       REG    8,2   278432  67117901 /usr/lib64/ld-2.28.so
tail    17292 root  mem       REG    8,2   337024 134352640 /usr/lib/locale/en_US.utf8/LC_CTYPE
tail    17292 root  mem       REG    8,2       54 134352643 /usr/lib/locale/en_US.utf8/LC_NUMERIC
tail    17292 root  mem       REG    8,2     3316 201334461 /usr/lib/locale/en_US.utf8/LC_TIME
tail    17292 root  mem       REG    8,2      286 201334459 /usr/lib/locale/en_US.utf8/LC_MONETARY
tail    17292 root  mem       REG    8,2       57 201334471 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
tail    17292 root  mem       REG    8,2       34  67117882 /usr/lib/locale/en_US.utf8/LC_PAPER
tail    17292 root  mem       REG    8,2       77 134352642 /usr/lib/locale/en_US.utf8/LC_NAME
tail    17292 root  mem       REG    8,2    26998  67152228 /usr/lib64/gconv/gconv-modules.cache
tail    17292 root  mem       REG    8,2      167 201334456 /usr/lib/locale/en_US.utf8/LC_ADDRESS
tail    17292 root  mem       REG    8,2       59 201334460 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
tail    17292 root  mem       REG    8,2       23 201334458 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
tail    17292 root  mem       REG    8,2      368 201334457 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
tail    17292 root    0u      CHR  136,2      0t0         5 /dev/pts/2
tail    17292 root    1u      CHR  136,2      0t0         5 /dev/pts/2
tail    17292 root    2u      CHR  136,2      0t0         5 /dev/pts/2
tail    17292 root    3r      REG    8,2   619555 134724368 /var/log/messages
tail    17292 root    4r  a_inode   0,14        0     10418 inotify

[root@rocky8 ~]# cat /proc/17292/fd/3

[root@rocky8 ~]# cat /proc/17292/fd/3 > /var/log/messages 
[root@rocky8 ~]# ll /var/log/messages 
-rw-------. 1 root root 0 Oct 31 19:04 /var/log/messages

范例:

#查看由登陆用户启动而非系统启动的进程
[root@rocky8 ~]# lsof /dev/pts/1
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    10452 root    0u   CHR  136,1      0t0    4 /dev/pts/1
bash    10452 root    1u   CHR  136,1      0t0    4 /dev/pts/1
bash    10452 root    2u   CHR  136,1      0t0    4 /dev/pts/1
bash    10452 root  255u   CHR  136,1      0t0    4 /dev/pts/1
vim     10704 root    0u   CHR  136,1      0t0    4 /dev/pts/1
vim     10704 root    1u   CHR  136,1      0t0    4 /dev/pts/1
vim     10704 root    2u   CHR  136,1      0t0    4 /dev/pts/1

#指定进程号,可以查看该进程打开的文件
[root@rocky8 ~]# lsof -p 22
COMMAND   PID USER   FD      TYPE DEVICE SIZE/OFF NODE NAME
kcompactd  22 root  cwd       DIR    8,2      256  128 /
kcompactd  22 root  rtd       DIR    8,2      256  128 /
kcompactd  22 root  txt   unknown                      /proc/22/exe

[root@rocky8 ~]# bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
[root@rocky8 ~]# lsof -p `pidof bc`
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
bc      17348 root  cwd    DIR    8,2      251 201326721 /root
bc      17348 root  rtd    DIR    8,2      256       128 /
bc      17348 root  txt    REG    8,2    92960    656596 /usr/bin/bc
bc      17348 root  mem    REG    8,2    28968  67117910 /usr/lib64/libdl-2.28.so
bc      17348 root  mem    REG    8,2  3168120  67117908 /usr/lib64/libc-2.28.so
bc      17348 root  mem    REG    8,2   187472  67117821 /usr/lib64/libtinfo.so.6.1
bc      17348 root  mem    REG    8,2   180224  67117811 /usr/lib64/libncurses.so.6.1
bc      17348 root  mem    REG    8,2   324992  67152362 /usr/lib64/libreadline.so.7.0
bc      17348 root  mem    REG    8,2   278432  67117901 /usr/lib64/ld-2.28.so
bc      17348 root  mem    REG    8,2    26998  67152228 /usr/lib64/gconv/gconv-modules.cache
bc      17348 root  mem    REG    8,2   337024 134352640 /usr/lib/locale/en_US.utf8/LC_CTYPE
bc      17348 root    0u   CHR  136,3      0t0         6 /dev/pts/3
bc      17348 root    1u   CHR  136,3      0t0         6 /dev/pts/3
bc      17348 root    2u   CHR  136,3      0t0         6 /dev/pts/3

#查看指定程序打开的文件
[root@rocky8 ~]# lsof -c mysqld
COMMAND   PID  USER   FD      TYPE             DEVICE SIZE/OFF      NODE NAME
mysqld  14755 mysql  cwd       DIR                8,2     4096 202180426 /var/lib/mysql
mysqld  14755 mysql  rtd       DIR                8,2      256       128 /
mysqld  14755 mysql  txt       REG                8,2 67443400 202180424 /usr/libexec/mysqld
mysqld  14755 mysql  DEL       REG               0,18              93283 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93282 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93281 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93280 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93279 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93278 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93277 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93276 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93275 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93274 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93273 /[aio]
mysqld  14755 mysql  DEL       REG               0,18              93272 /[aio]
mysqld  14755 mysql  mem       REG                8,2    42872  68290771 /usr/lib64/mysql/plugin/component_reference_cache.so
mysqld  14755 mysql  mem       REG                8,2    83656  67117920 /usr/lib64/libnss_files-2.28.so
mysqld  14755 mysql  mem       REG                8,2  3168120  67117908 /usr/lib64/libc-2.28.so
mysqld  14755 mysql  mem       REG                8,2    99656  67108999 /usr/lib64/libgcc_s-8-20200928.so.1
mysqld  14755 mysql  mem       REG                8,2  2191808  67117912 /usr/lib64/libm-2.28.so
mysqld  14755 mysql  mem       REG                8,2  1661392  67152370 /usr/lib64/libstdc++.so.6.0.25
mysqld  14755 mysql  mem       REG                8,2    51656  67196504 /usr/lib64/libnuma.so.1.0.0
mysqld  14755 mysql  mem       REG                8,2   119672  67188491 /usr/lib64/liblz4.so.1.8.3
mysqld  14755 mysql  mem       REG                8,2    95456  67152235 /usr/lib64/libz.so.1.2.11
mysqld  14755 mysql  mem       REG                8,2    12264  68277612 /usr/lib64/libaio.so.1.0.1
mysqld  14755 mysql  mem       REG                8,2    28968  67117910 /usr/lib64/libdl-2.28.so
mysqld  14755 mysql  mem       REG                8,2  3071432  67582048 /usr/lib64/libcrypto.so.1.1.1g
mysqld  14755 mysql  mem       REG                8,2   615576  67582050 /usr/lib64/libssl.so.1.1.1g
mysqld  14755 mysql  mem       REG                8,2    68944  67117926 /usr/lib64/librt-2.28.so
mysqld  14755 mysql  mem       REG                8,2   460704  68256758 /usr/lib64/libprotobuf-lite.so.15.0.0
mysqld  14755 mysql  mem       REG                8,2    11640  67682224 /usr/lib64/libevent_pthreads-2.1.so.6.0.2
mysqld  14755 mysql  mem       REG                8,2    28560  67682222 /usr/lib64/libevent_openssl-2.1.so.6.0.2
mysqld  14755 mysql  mem       REG                8,2   149904  67682220 /usr/lib64/libevent_extra-2.1.so.6.0.2
mysqld  14755 mysql  mem       REG                8,2   236880  67682218 /usr/lib64/libevent_core-2.1.so.6.0.2
mysqld  14755 mysql  mem       REG                8,2   320504  67117922 /usr/lib64/libpthread-2.28.so
mysqld  14755 mysql  mem       REG                8,2   278432  67117901 /usr/lib64/ld-2.28.so
mysqld  14755 mysql    0r      CHR                1,3      0t0     10424 /dev/null
mysqld  14755 mysql    1w      REG                8,2     1945 134800202 /var/log/mysql/mysqld.log
mysqld  14755 mysql    2w      REG                8,2     1945 134800202 /var/log/mysql/mysqld.log
mysqld  14755 mysql    3u     unix 0xffff988584e4cc80      0t0     90871 type=DGRAM
mysqld  14755 mysql    4u      REG                8,2       16 201878694 /var/lib/mysql/binlog.index
mysqld  14755 mysql    5uW     REG                8,2 50331648 201878660 /var/lib/mysql/ib_logfile0
mysqld  14755 mysql    6u      REG                8,2        0 134651604 /var/tmp/#134651604 (deleted)
mysqld  14755 mysql    7u      REG                8,2        0 134651605 /var/tmp/#134651605 (deleted)
mysqld  14755 mysql    8u      REG                8,2        0 134651606 /var/tmp/#134651606 (deleted)
mysqld  14755 mysql    9uW     REG                8,2 12582912 201878659 /var/lib/mysql/ibdata1
mysqld  14755 mysql   10uW     REG                8,2 50331648 201878661 /var/lib/mysql/ib_logfile1
mysqld  14755 mysql   11uW     REG                8,2   196608 201878662 /var/lib/mysql/#ib_16384_0.dblwr
mysqld  14755 mysql   12uW     REG                8,2  8585216 201878663 /var/lib/mysql/#ib_16384_1.dblwr
mysqld  14755 mysql   13uW     REG                8,2 16777216 201878665 /var/lib/mysql/undo_001
mysqld  14755 mysql   14u      REG                8,2        0 134651607 /var/tmp/#134651607 (deleted)
mysqld  14755 mysql   15uW     REG                8,2 16777216 201878667 /var/lib/mysql/undo_002
mysqld  14755 mysql   16uW     REG                8,2 12582912 201878671 /var/lib/mysql/ibtmp1
mysqld  14755 mysql   17uW     REG                8,2 28311552 201878664 /var/lib/mysql/mysql.ibd
mysqld  14755 mysql   18u  a_inode               0,14        0     10418 [eventpoll]
mysqld  14755 mysql   19r     FIFO               0,13      0t0     90874 pipe
mysqld  14755 mysql   20w     FIFO               0,13      0t0     90874 pipe
mysqld  14755 mysql   21u  a_inode               0,14        0     10418 [eventfd]
mysqld  14755 mysql   22u     IPv6              90876      0t0       TCP *:mysqlx (LISTEN)
mysqld  14755 mysql   23u     unix 0xffff988584e48000      0t0     90877 /var/lib/mysql/mysqlx.sock type=STREAM
mysqld  14755 mysql   24uW     REG                8,2    81920 201878689 /var/lib/mysql/#innodb_temp/temp_10.ibt
mysqld  14755 mysql   25u     IPv6              90879      0t0       TCP *:mysql (LISTEN)
mysqld  14755 mysql   26w      REG                8,2      156 201878693 /var/lib/mysql/binlog.000001
mysqld  14755 mysql   27u     unix 0xffff988584e4e300      0t0     90880 /var/lib/mysql/mysql.sock type=STREAM
mysqld  14755 mysql   28uW     REG                8,2    81920 201878672 /var/lib/mysql/#innodb_temp/temp_1.ibt
mysqld  14755 mysql   29uW     REG                8,2    81920 201878673 /var/lib/mysql/#innodb_temp/temp_2.ibt
mysqld  14755 mysql   30uW     REG                8,2    81920 201878674 /var/lib/mysql/#innodb_temp/temp_3.ibt
mysqld  14755 mysql   31uW     REG                8,2    81920 201878675 /var/lib/mysql/#innodb_temp/temp_4.ibt
mysqld  14755 mysql   32uW     REG                8,2    81920 201878676 /var/lib/mysql/#innodb_temp/temp_5.ibt
mysqld  14755 mysql   33uW     REG                8,2    81920 201878677 /var/lib/mysql/#innodb_temp/temp_6.ibt
mysqld  14755 mysql   34uW     REG                8,2    81920 201878678 /var/lib/mysql/#innodb_temp/temp_7.ibt
mysqld  14755 mysql   35uW     REG                8,2    81920 201878679 /var/lib/mysql/#innodb_temp/temp_8.ibt
mysqld  14755 mysql   36uW     REG                8,2    81920 201878688 /var/lib/mysql/#innodb_temp/temp_9.ibt


[root@rocky8 ~]# lsof -c bc
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
bc      17348 root  cwd    DIR    8,2      251 201326721 /root
bc      17348 root  rtd    DIR    8,2      256       128 /
bc      17348 root  txt    REG    8,2    92960    656596 /usr/bin/bc
bc      17348 root  mem    REG    8,2    28968  67117910 /usr/lib64/libdl-2.28.so
bc      17348 root  mem    REG    8,2  3168120  67117908 /usr/lib64/libc-2.28.so
bc      17348 root  mem    REG    8,2   187472  67117821 /usr/lib64/libtinfo.so.6.1
bc      17348 root  mem    REG    8,2   180224  67117811 /usr/lib64/libncurses.so.6.1
bc      17348 root  mem    REG    8,2   324992  67152362 /usr/lib64/libreadline.so.7.0
bc      17348 root  mem    REG    8,2   278432  67117901 /usr/lib64/ld-2.28.so
bc      17348 root  mem    REG    8,2    26998  67152228 /usr/lib64/gconv/gconv-modules.cache
bc      17348 root  mem    REG    8,2   337024 134352640 /usr/lib/locale/en_US.utf8/LC_CTYPE
bc      17348 root    0u   CHR  136,3      0t0         6 /dev/pts/3
bc      17348 root    1u   CHR  136,3      0t0         6 /dev/pts/3
bc      17348 root    2u   CHR  136,3      0t0         6 /dev/pts/3


#查看指定用户打开的文件
[root@rocky8 ~]# lsof -u root |less
COMMAND     PID USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
systemd       1 root  cwd       DIR                8,2      256        128 /
systemd       1 root  rtd       DIR                8,2      256        128 /
systemd       1 root  txt       REG                8,2  1589040     484070 /usr/lib/systemd/systemd
systemd       1 root  mem       REG                8,2   147360   67167846 /usr/lib64/libnl-3.so.200.26.0
systemd       1 root  mem       REG                8,2   549872   67167854 /usr/lib64/libnl-route-3.so.200.26.0
systemd       1 root  mem       REG                8,2   131144   67543778 /usr/lib64/libibverbs.so.1.11.32.0
systemd       1 root  mem       REG                8,2  2191808   67117912 /usr/lib64/libm-2.28.so
systemd       1 root  mem       REG                8,2   633032   67556008 /usr/lib64/libudev.so.1.6.11
systemd       1 root  mem       REG                8,2   739264   67152232 /usr/lib64/libsepol.so.1
systemd       1 root  mem       REG                8,2  1580488   67167863 /usr/lib64/libunistring.so.2.1.0
systemd       1 root  mem       REG                8,2   312496   67543796 /usr/lib64/libpcap.so.1.9.1
systemd       1 root  mem       REG                8,2   138792   67152254 /usr/lib64/libgpg-error.so.0.24.2
systemd       1 root  mem       REG                8,2    66744   67167805 /usr/lib64/libjson-c.so.4.0.0
systemd       1 root  mem       REG                8,2   371208   67582024 /usr/lib64/libdevmapper.so.1.02
systemd       1 root  mem       REG                8,2    24784   67167807 /usr/lib64/libattr.so.1.1.2448
systemd       1 root  mem       REG                8,2  3071432   67582048 /usr/lib64/libcrypto.so.1.1.1g
systemd       1 root  mem       REG                8,2   615576   67582050 /usr/lib64/libssl.so.1.1.1g
systemd       1 root  mem       REG                8,2    95456   67152235 /usr/lib64/libz.so.1.2.11
systemd       1 root  mem       REG                8,2    25112   67167753 /usr/lib64/libcap-ng.so.0.0.0
systemd       1 root  mem       REG                8,2    33384   67152345 /usr/lib64/libuuid.so.1.3.0
systemd       1 root  mem       REG                8,2    28968   67117910 /usr/lib64/libdl-2.28.so
systemd       1 root  mem       REG                8,2   543152   67117790 /usr/lib64/libpcre2-8.so.0.7.1
systemd       1 root  mem       REG                8,2   343608   67556010 /usr/lib64/libblkid.so.1.1.0
systemd       1 root  mem       REG                8,2   119672   67188491 /usr/lib64/liblz4.so.1.8.3
systemd       1 root  mem       REG                8,2   162224   67152239 /usr/lib64/liblzma.so.5.2.4

#查看指定目录下被打开的文件,参数+D为递归列出目录下被打开的文件,参数+d为列出目录下被打开的文件
[root@rocky8 ~]# lsof +D /var/log/
COMMAND     PID  USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
auditd      685  root    6w   REG    8,2   526438 201929291 /var/log/audit/audit.log
VGAuthSer   718  root    2w   REG    8,2     6486 134328706 /var/log/vmware-vgauthsvc.log.0
VGAuthSer   718  root    4w   REG    8,2     6486 134328706 /var/log/vmware-vgauthsvc.log.0
vmtoolsd    719  root    4w   REG    8,2     7915 134328707 /var/log/vmware-vmsvc-root.log
sssd        721  root    3w   REG    8,2     3593    505665 /var/log/sssd/sssd.log
tuned       767  root    4w   REG    8,2     5609  67632835 /var/log/tuned/tuned.log
sssd_be     773  root    6w   REG    8,2    12023    505670 /var/log/sssd/sssd_implicit_files.log
sssd_nss    809  root    6w   REG    8,2     6488    505671 /var/log/sssd/sssd_nss.log
rsyslogd    837  root    5w   REG    8,2     7764 134724369 /var/log/secure
rsyslogd    837  root    7w   REG    8,2        0 134724368 /var/log/messages
rsyslogd    837  root    8w   REG    8,2     3676 134328713 /var/log/cron
mysqld    14755 mysql    1w   REG    8,2     1945 134800202 /var/log/mysql/mysqld.log
mysqld    14755 mysql    2w   REG    8,2     1945 134800202 /var/log/mysql/mysqld.log
[root@rocky8 ~]# lsof +d /var/log/
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
VGAuthSer 718 root    2w   REG    8,2     6486 134328706 /var/log/vmware-vgauthsvc.log.0
VGAuthSer 718 root    4w   REG    8,2     6486 134328706 /var/log/vmware-vgauthsvc.log.0
vmtoolsd  719 root    4w   REG    8,2     7915 134328707 /var/log/vmware-vmsvc-root.log
rsyslogd  837 root    5w   REG    8,2     7764 134724369 /var/log/secure
rsyslogd  837 root    7w   REG    8,2        0 134724368 /var/log/messages
rsyslogd  837 root    8w   REG    8,2     3676 134328713 /var/log/cron

#查看所有网络连接,通过参数-i查看网络连接的情况,包括连接的ip、端口等以及一些服务的连接情况,例如:sshd等。也可以通过指定ip查看该ip的网络连接情况
lsof -i –n
lsof -i@127.0.0.1

#查看端口连接情况,通过参数-i:端口可以查看端口的占用情况,-i参数还有查看协议,ip的连接情况等
[root@rocky8 ~]# lsof -i :80 -n

#查看指定进程打开的网络连接,参数-i、-a、-p等,-i查看网络连接情况,-a查看存在的进程,-p指定进程
lsof -i –n -a -p 9527

#查看指定状态的网络连接,-n:no host names, -P:no port names,-i TCP指定协议,-s指定协议状态通过多个参数可以清晰的查看网络连接情况、协议连接情况等
[root@rocky8 ~]# lsof -n -P -i TCP -s TCP:ESTABLISHED
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    10186 root    5u  IPv4  72386      0t0  TCP 172.31.1.8:22->172.31.0.1:58670 (ESTABLISHED)
sshd    10188 root    5u  IPv4  72386      0t0  TCP 172.31.1.8:22->172.31.0.1:58670 (ESTABLISHED)
sshd    10449 root    5u  IPv4  73393      0t0  TCP 172.31.1.8:22->172.31.0.1:58954 (ESTABLISHED)
sshd    10451 root    5u  IPv4  73393      0t0  TCP 172.31.1.8:22->172.31.0.1:58954 (ESTABLISHED)
sshd    10481 root    5u  IPv4  73590      0t0  TCP 172.31.1.8:22->172.31.0.1:58955 (ESTABLISHED)
sshd    10483 root    5u  IPv4  73590      0t0  TCP 172.31.1.8:22->172.31.0.1:58955 (ESTABLISHED)
sshd    17293 root    5u  IPv4 176848      0t0  TCP 172.31.1.8:22->172.31.0.1:53960 (ESTABLISHED)
sshd    17295 root    5u  IPv4 176848      0t0  TCP 172.31.1.8:22->172.31.0.1:53960 (ESTABLISHED)

范例:利用 lsof 恢复正在使用中的误删除的文件

[root@rocky8 ~]# lsof |grep /var/log/messages
rsyslogd    837                    root    7w      REG                8,2        0  134724368 /var/log/messages
rsyslogd    837   850 in:imjour    root    7w      REG                8,2        0  134724368 /var/log/messages
rsyslogd    837   852 rs:main      root    7w      REG                8,2        0  134724368 /var/log/messages

[root@rocky8 ~]# rm -f /var/log/messages

[root@rocky8 ~]# lsof |grep /var/log/messages
rsyslogd    837                    root    7w      REG                8,2        0  134724368 /var/log/messages (deleted)
rsyslogd    837   850 in:imjour    root    7w      REG                8,2        0  134724368 /var/log/messages (deleted)
rsyslogd    837   852 rs:main      root    7w      REG                8,2        0  134724368 /var/log/messages (deleted)

[root@rocky8 ~]# cat /proc/837/fd/8

[root@rocky8 ~]# cat /proc/837/fd/8 > /var/log/messages

2.19 CentOS 8 新特性 cockpit

Cockpit 是CentOS 8 取入的新特性,是一个基于 Web 界面的应用,它提供了对系统的图形化管理

范例:安装 cockpit

[root@rocky8 ~]# dnf -y install cockpit;systemctl start cockpit

[root@rocky8 ~]# ss -ntl
State           Recv-Q          Send-Q                     Local Address:Port                      Peer Address:Port          Process          
LISTEN          0               128                              0.0.0.0:22                             0.0.0.0:*                              
LISTEN          0               128                                    *:9090                                 *:*                              
LISTEN          0               70                                     *:33060                                *:*                              
LISTEN          0               128                                    *:3306                                 *:*                              
LISTEN          0               128                                 [::]:22                                [::]:*  

打开浏览器,访问以下地址:

https://centos8主机:9090

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.20 信号发送 kill

kill:内部命令,可用来向进程发送控制信号,以实现对进程管理,每个信号对应一个数字,信号名称以SIG开头(可省略),不区分大小写

显示当前系统可用信号:

kill -l
trap -l

查看帮助:man 7 signal

常用信号:

1) SIGHUP 无须关闭进程而让其重读配置文件
2) SIGINT 中止正在运行的进程;相当于Ctrl+c
3) SIGQUIT 相当于ctrl+\
9) SIGKILL 强制杀死正在运行的进程,此信号慎用!
15) SIGTERM 终止正在运行的进程,默认信号
18) SIGCONT 继续运行
19) SIGSTOP 后台休眠,处于stop状态

指定信号的方法 :

向进程发送信号:

按PID:

kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

范例:

kill -1 pid …
kill -n 9 pid
kill -s SIGINT pid

按名称:killall 来自于psmisc包

killall [-SIGNAL] comm…

按模式:

pkill [options] pattern

常用选项

-SIGNAL
-u uid: effective user,生效者
-U uid: real user,真正发起运行命令者
-t terminal: 与指定终端相关的进程
-l: 显示进程名(pgrep可用)
-a: 显示完整格式的进程名(pgrep可用)
-P pid: 显示指定进程的子进程

范例:

[root@rocky8 ~]# kill -L
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.368 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.291 ms
...

[root@rocky8 ~]# kill `pidof ping` #默认就是-15 正常结束,可以不用写

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.368 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.291 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=1.19 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.317 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.290 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.333 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.329 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=1.54 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.423 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.278 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.555 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=2.04 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.235 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.250 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=1.32 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.302 ms
64 bytes from 172.31.0.7: icmp_seq=17 ttl=64 time=1.75 ms
64 bytes from 172.31.0.7: icmp_seq=18 ttl=64 time=0.303 ms
64 bytes from 172.31.0.7: icmp_seq=19 ttl=64 time=0.245 ms
64 bytes from 172.31.0.7: icmp_seq=20 ttl=64 time=2.40 ms
64 bytes from 172.31.0.7: icmp_seq=21 ttl=64 time=1.39 ms
64 bytes from 172.31.0.7: icmp_seq=22 ttl=64 time=0.187 ms
Terminated

[root@rocky8 ~]# pidof bash
17296 10770 10484 10452 10189
[root@rocky8 ~]# ps
    PID TTY          TIME CMD
  10189 pts/2    00:00:00 bash
  21327 pts/2    00:00:00 ps
[root@rocky8 ~]# kill 10189
#bash进程正常杀不了

[root@rocky8 ~]# kill -9 10189 #-9 强制杀掉进程

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=1.21 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.325 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.224 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.257 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.184 ms

[root@rocky8 ~]# killall ping #killall  进程名   可以直接结束

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=1.21 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.325 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.224 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.257 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.184 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.223 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.255 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.223 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.409 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.455 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.432 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.356 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.258 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=1.24 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.326 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.396 ms
Terminated


范例:查看HUP信号

#许多服务的支持的reload操作,实际就是发送了HUP信号
#service httpd reload 即相当于 killall -1 httpd
[root@centos6 ~]# yum -y install httpd
[root@centos6 ~]# grep -A 10 -w reload -m 1 /etc/init.d/httpd
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then

范例:利用 0 信号实现进程的健康性检查

[root@rocky8 ~]# man kill
       If signal is 0, then no actual signal is sent, but error checking is still performed.
# kill -0 pid 不发送任何信号,但是系统会进行错误检查。
#所以经常用来检查一个进程是否存在,存在返回0;不存在返回1;

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.269 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.526 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.288 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.624 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=1.13 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.216 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.709 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.583 ms

[root@rocky8 ~]# killall -0 ping #0不发送任何信号,但是会检查
[root@rocky8 ~]# echo $? #返回0 ,说明进程是健康的
0

[root@rocky8 ~]# killall ping
[root@rocky8 ~]# killall -0 ping
ping: no process found
[root@rocky8 ~]# echo $? #返回1,进程结束
1

#此方式有局限性,即使进程处于停止或僵尸状态,此方式仍然认为是进程是健康的

范例:

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.281 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=1.09 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.257 ms

[root@rocky8 ~]# kill -s SIGKILL `pidof ping`

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.281 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=1.09 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.257 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.222 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.277 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.473 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.340 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.337 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.578 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.567 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.391 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.215 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.603 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.243 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.218 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.350 ms
64 bytes from 172.31.0.7: icmp_seq=17 ttl=64 time=0.562 ms
64 bytes from 172.31.0.7: icmp_seq=18 ttl=64 time=0.763 ms
64 bytes from 172.31.0.7: icmp_seq=19 ttl=64 time=0.412 ms
64 bytes from 172.31.0.7: icmp_seq=20 ttl=64 time=0.521 ms
Killed

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.266 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.443 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.397 ms

[root@rocky8 ~]# kill -SIGKILL `pidof ping`

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.266 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.443 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.397 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.545 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.819 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.231 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.306 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.248 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.205 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.232 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.470 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.554 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.262 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.382 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.499 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.189 ms
64 bytes from 172.31.0.7: icmp_seq=17 ttl=64 time=0.245 ms
64 bytes from 172.31.0.7: icmp_seq=18 ttl=64 time=0.490 ms
64 bytes from 172.31.0.7: icmp_seq=19 ttl=64 time=0.534 ms
64 bytes from 172.31.0.7: icmp_seq=20 ttl=64 time=0.242 ms
Killed

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.284 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.188 ms

[root@rocky8 ~]# kill -KILL `pidof ping`

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.284 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.188 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.271 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.186 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.238 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.211 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.326 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.348 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.498 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.363 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.430 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.300 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.222 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.278 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.797 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.355 ms
Killed

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.433 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.499 ms

[root@rocky8 ~]# kill -9 `pidof ping`

[root@rocky8 ~]# ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.433 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.499 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.386 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.307 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.246 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.584 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.332 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=1.04 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.220 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.448 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.218 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.278 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.387 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.539 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.377 ms
Killed

[root@rocky8 ~]# su - raymond
Last login: Sun Oct 31 16:49:34 CST 2021 on pts/0
[raymond@rocky8 ~]$ ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.292 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.245 ms

[root@rocky8 ~]# pkill -u raymond

[raymond@rocky8 ~]$ ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.292 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.245 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.801 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.243 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.214 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.220 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.262 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.259 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.432 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.476 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.201 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.419 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.514 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.265 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.292 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.416 ms
64 bytes from 172.31.0.7: icmp_seq=17 ttl=64 time=0.298 ms
64 bytes from 172.31.0.7: icmp_seq=18 ttl=64 time=0.230 ms
64 bytes from 172.31.0.7: icmp_seq=19 ttl=64 time=0.417 ms
64 bytes from 172.31.0.7: icmp_seq=20 ttl=64 time=0.230 ms
64 bytes from 172.31.0.7: icmp_seq=21 ttl=64 time=0.217 ms
Terminated

[raymond@rocky8 ~]$ ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.340 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.265 ms

[root@rocky8 ~]# pkill -u -9 raymond

[raymond@rocky8 ~]$ ping 172.31.0.7
PING 172.31.0.7 (172.31.0.7) 56(84) bytes of data.
64 bytes from 172.31.0.7: icmp_seq=1 ttl=64 time=0.340 ms
64 bytes from 172.31.0.7: icmp_seq=2 ttl=64 time=0.265 ms
64 bytes from 172.31.0.7: icmp_seq=3 ttl=64 time=0.450 ms
64 bytes from 172.31.0.7: icmp_seq=4 ttl=64 time=0.228 ms
64 bytes from 172.31.0.7: icmp_seq=5 ttl=64 time=0.595 ms
64 bytes from 172.31.0.7: icmp_seq=6 ttl=64 time=0.551 ms
64 bytes from 172.31.0.7: icmp_seq=7 ttl=64 time=0.242 ms
64 bytes from 172.31.0.7: icmp_seq=8 ttl=64 time=0.368 ms
64 bytes from 172.31.0.7: icmp_seq=9 ttl=64 time=0.276 ms
64 bytes from 172.31.0.7: icmp_seq=10 ttl=64 time=0.551 ms
64 bytes from 172.31.0.7: icmp_seq=11 ttl=64 time=0.230 ms
64 bytes from 172.31.0.7: icmp_seq=12 ttl=64 time=0.697 ms
64 bytes from 172.31.0.7: icmp_seq=13 ttl=64 time=0.538 ms
64 bytes from 172.31.0.7: icmp_seq=14 ttl=64 time=0.485 ms
64 bytes from 172.31.0.7: icmp_seq=15 ttl=64 time=0.739 ms
64 bytes from 172.31.0.7: icmp_seq=16 ttl=64 time=0.460 ms
64 bytes from 172.31.0.7: icmp_seq=17 ttl=64 time=0.516 ms
64 bytes from 172.31.0.7: icmp_seq=18 ttl=64 time=0.395 ms
64 bytes from 172.31.0.7: icmp_seq=19 ttl=64 time=0.216 ms
Killed

2.21 作业管理

Linux的作业控制

后台作业虽然被送往后台运行,但其依然与终端相关;退出终端,将关闭后台作业。如果希望送往后台后,剥离与终端的关系

查看当前终端所有作业:

jobs

作业控制:

fg [[%]JOB_NUM]:把指定的后台作业调回前台
bg [[%]JOB_NUM]:让送往后台的作业在后台继续运行
kill [%JOB_NUM]: 终止指定的作业

范例:

[root@rocky8 ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.026 ms
^C
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1021ms
rtt min/avg/max/mdev = 0.023/0.024/0.026/0.005 ms

[root@rocky8 ~]# ping 127.0.0.1&
[1] 22545
[root@rocky8 ~]# PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.027 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.047 ms

#关掉窗口
[root@rocky8 ~]# pstree -p |grep ping
           |-ping(22487)

[root@rocky8 ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.029 ms
^Z
[1]+  Stopped                 ping 127.0.0.1 #ctrl+z 后台停止

[root@rocky8 ~]# ps aux |grep ping
root       22487  0.0  0.2  32448  2304 ?        S    20:00   0:00 ping 127.0.0.1
root       22607  0.0  0.3  32448  2492 pts/0    T    20:04   0:00 ping 127.0.0.1
root       22609  0.0  0.1  12136  1152 pts/0    S+   20:04   0:00 grep --color=auto ping

[root@rocky8 ~]# jobs
[1]+  Stopped                 ping 127.0.0.1


[root@rocky8 ~]# ping 127.2 &
[2] 4274
[root@rocky8 ~]# PING 127.2 (127.0.0.2) 56(84) bytes of data.
64 bytes from 127.0.0.2: icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from 127.0.0.2: icmp_seq=2 ttl=64 time=0.049 ms
jo64 bytes from 127.0.0.2: icmp_seq=3 ttl=64 time=0.046 ms
bs64 bytes from 127.0.0.2: icmp_seq=4 ttl=64 time=0.051 ms
64 bytes from 127.0.0.2: icmp_seq=5 ttl=64 time=0.046 ms

[1]+  Stopped                 ping 127.0.0.1
[2]-  Running                 ping 127.2 &

[root@rocky8 ~]# kill -19 %2  #把后台运行的程序变成后台停止

[root@rocky8 ~]# jobs
[1]-  Stopped                 ping 127.0.0.1
[2]+  Stopped                 ping 127.2

[root@rocky8 ~]# bg 1 #把作业编号1,从停止态变成运行态
[1]- ping 127.0.0.1 &
[root@rocky8 ~]# 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.027 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.027 ms

kill -18 %2 #把作业编号2,从停止态变成运行态
fg %1	#把作业编号1,从后台变成前台
fg %2	#把作业编号2,从后台变成前台

2.22 并行运行

利用后台执行,实现并行功能,即同时运行多个进程,提高效率

方法1

cat all.sh
f1.sh&
f2.sh&
f3.sh&

方法2

(f1.sh&);(f2.sh&);(f3.sh&)

方法3

f1.sh&f2.sh&f3.sh&

范例:多组命令实现并行

[root@rocky8 ~]# { ping -c3 127.1; ping 127.2; }& { ping -c3 127.3 ;ping 127.4; }&

范例:

[root@rocky8 ~]# vim check_host.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-31
#FileName:      check_host.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
net=172.31.0
for i in {1..254};do
    { ping -c1 -w1 $net.$i &>/dev/null && echo $net.$i is up | tee -a hostlist.log || echo $net.$i is down;}&
done
wait

[root@rocky8 ~]# bash check_host.sh 
172.31.0.6 is up
172.31.0.7 is up
172.31.0.2 is up
172.31.0.4 is down
172.31.0.8 is down
172.31.0.1 is down
172.31.0.14 is down
172.31.0.16 is down
172.31.0.39 is down
172.31.0.33 is down
172.31.0.29 is down
172.31.0.42 is down
172.31.0.22 is down
172.31.0.18 is down
172.31.0.31 is down
172.31.0.24 is down
172.31.0.20 is down
172.31.0.27 is down
172.31.0.35 is down
172.31.0.37 is down
172.31.0.44 is down
172.31.0.47 is down
172.31.0.50 is down
172.31.0.10 is down
172.31.0.57 is down
172.31.0.67 is down
172.31.0.62 is down
172.31.0.3 is down
172.31.0.19 is down
172.31.0.28 is down
172.31.0.64 is down
172.31.0.73 is down
172.31.0.93 is down
172.31.0.12 is down
172.31.0.15 is down
172.31.0.84 is down
172.31.0.45 is down
172.31.0.86 is down
172.31.0.36 is down
172.31.0.32 is down
172.31.0.21 is down
172.31.0.69 is down
172.31.0.78 is down
172.31.0.25 is down
172.31.0.114 is down
172.31.0.123 is down
172.31.0.209 is down
172.31.0.179 is down
172.31.0.222 is down
172.31.0.206 is down
172.31.0.88 is down
172.31.0.150 is down
172.31.0.98 is down
172.31.0.249 is down
172.31.0.251 is down
172.31.0.149 is down
172.31.0.168 is down
172.31.0.99 is down
172.31.0.115 is down
172.31.0.96 is down
172.31.0.243 is down
172.31.0.177 is down
172.31.0.159 is down
172.31.0.241 is down
172.31.0.140 is down
172.31.0.90 is down
172.31.0.205 is down
172.31.0.130 is down
172.31.0.40 is down
172.31.0.246 is down
172.31.0.54 is down
172.31.0.122 is down
172.31.0.11 is down
172.31.0.58 is down
172.31.0.70 is down
172.31.0.139 is down
172.31.0.156 is down
172.31.0.80 is down
172.31.0.52 is down
172.31.0.81 is down
172.31.0.49 is down
172.31.0.60 is down
172.31.0.59 is down
172.31.0.121 is down
172.31.0.181 is down
172.31.0.74 is down
172.31.0.66 is down
172.31.0.83 is down
172.31.0.195 is down
172.31.0.23 is down
172.31.0.161 is down
172.31.0.65 is down
172.31.0.17 is down
172.31.0.153 is down
172.31.0.75 is down
172.31.0.79 is down
172.31.0.132 is down
172.31.0.104 is down
172.31.0.53 is down
172.31.0.190 is down
172.31.0.211 is down
172.31.0.134 is down
172.31.0.217 is down
172.31.0.240 is down
172.31.0.148 is down
172.31.0.55 is down
172.31.0.238 is down
172.31.0.165 is down
172.31.0.63 is down
172.31.0.210 is down
172.31.0.171 is down
172.31.0.248 is down
172.31.0.189 is down
172.31.0.250 is down
172.31.0.127 is down
172.31.0.43 is down
172.31.0.136 is down
172.31.0.105 is down
172.31.0.82 is down
172.31.0.203 is down
172.31.0.116 is down
172.31.0.107 is down
172.31.0.71 is down
172.31.0.131 is down
172.31.0.175 is down
172.31.0.141 is down
172.31.0.77 is down
172.31.0.125 is down
172.31.0.48 is down
172.31.0.146 is down
172.31.0.110 is down
172.31.0.109 is down
172.31.0.223 is down
172.31.0.184 is down
172.31.0.225 is down
172.31.0.200 is down
172.31.0.183 is down
172.31.0.97 is down
172.31.0.234 is down
172.31.0.144 is down
172.31.0.187 is down
172.31.0.226 is down
172.31.0.182 is down
172.31.0.152 is down
172.31.0.145 is down
172.31.0.85 is down
172.31.0.129 is down
172.31.0.120 is down
172.31.0.178 is down
172.31.0.253 is down
172.31.0.103 is down
172.31.0.202 is down
172.31.0.143 is down
172.31.0.138 is down
172.31.0.185 is down
172.31.0.244 is down
172.31.0.68 is down
172.31.0.111 is down
172.31.0.89 is down
172.31.0.215 is down
172.31.0.163 is down
172.31.0.214 is down
172.31.0.119 is down
172.31.0.213 is down
172.31.0.218 is down
172.31.0.128 is down
172.31.0.30 is down
172.31.0.135 is down
172.31.0.72 is down
172.31.0.26 is down
172.31.0.193 is down
172.31.0.94 is down
172.31.0.194 is down
172.31.0.51 is down
172.31.0.38 is down
172.31.0.87 is down
172.31.0.221 is down
172.31.0.34 is down
172.31.0.108 is down
172.31.0.76 is down
172.31.0.56 is down
172.31.0.92 is down
172.31.0.224 is down
172.31.0.46 is down
172.31.0.91 is down
172.31.0.157 is down
172.31.0.41 is down
172.31.0.188 is down
172.31.0.61 is down
172.31.0.254 is down
172.31.0.112 is down
172.31.0.169 is down
172.31.0.237 is down
172.31.0.160 is down
172.31.0.173 is down
172.31.0.158 is down
172.31.0.154 is down
172.31.0.176 is down
172.31.0.106 is down
172.31.0.118 is down
172.31.0.230 is down
172.31.0.229 is down
172.31.0.167 is down
172.31.0.100 is down
172.31.0.95 is down
172.31.0.232 is down
172.31.0.172 is down
172.31.0.245 is down
172.31.0.133 is down
172.31.0.137 is down
172.31.0.170 is down
172.31.0.191 is down
172.31.0.113 is down
172.31.0.126 is down
172.31.0.252 is down
172.31.0.174 is down
172.31.0.235 is down
172.31.0.212 is down
172.31.0.247 is down
172.31.0.242 is down
172.31.0.192 is down
172.31.0.228 is down
172.31.0.204 is down
172.31.0.151 is down
172.31.0.216 is down
172.31.0.236 is down
172.31.0.101 is down
172.31.0.231 is down
172.31.0.199 is down
172.31.0.233 is down
172.31.0.208 is down
172.31.0.207 is down
172.31.0.220 is down
172.31.0.239 is down
172.31.0.124 is down
172.31.0.13 is down
172.31.0.180 is down
172.31.0.227 is down
172.31.0.186 is down
172.31.0.5 is down
172.31.0.198 is down
172.31.0.162 is down
172.31.0.219 is down
172.31.0.117 is down
172.31.0.147 is down
172.31.0.155 is down
172.31.0.102 is down
172.31.0.197 is down
172.31.0.142 is down
172.31.0.166 is down
172.31.0.9 is down
172.31.0.196 is down
172.31.0.201 is down
172.31.0.164 is down

[root@rocky8 ~]# cat hostlist.log
172.31.0.6 is up
172.31.0.7 is up
172.31.0.2 is up

3.任务计划

通过任务计划,可以让系统自动的按时间或周期性任务执行任务

未来的某时间点执行一次任务

周期性运行某任务

3.1 一次性任务

at 工具

at 命令:

at [option] TIME

常用选项:

-V 显示版本信息
-t time 时间格式 [[CC]YY]MMDDhhmm[.ss]
-l 列出指定队列中等待运行的作业;相当于atq
-d N 删除指定的N号作业;相当于atrm
-c N 查看具体作业N号任务
-f file 指定的文件中读取任务
-m 当任务被完成之后,将给用户发送邮件,即使没有标准输出

注意:

作业执行命令的结果中的标准输出和错误以执行任务的用户身份发邮件通知给 root

默认CentOS 8 最小化安装没有安装邮件服务,需要自行安装

范例:

[root@rocky8 ~]# dnf install postfix -y;systemctl enable --now postfix

TIME:定义出什么时候进行 at 这项任务的时间

HH:MM [YYYY-mm-dd]
noon, midnight, teatime(4pm)
tomorrow
now+#{minutes,hours,days, OR weeks}

范例:at 时间格式

HH:MM 在今日的 HH:MM 进行,若该时刻已过,则明天此时执行任务
02:00

HH:MM YYYY-MM-DD 规定在某年某月的某一天的特殊时刻进行该项任务
02:00 2016-09-20

HH:MM[am|pm] [Month] [Date]
06pm March 17
17:20 tomorrow

HH:MM[am|pm] + number [minutes|hours|days|weeks], 在某个时间点再加几个时间后才进行该项任务
now + 5 min
02pm + 3 days

at 任务执行方式:

/etc/at.{allow,deny} 控制用户是否能执行at任务

范例:

[root@rocky8 ~]# at
-bash: at: command not found
[root@rocky8 ~]# dnf provides at
Last metadata expiration check: 2:51:45 ago on Sun 31 Oct 2021 05:56:28 PM CST.
at-3.1.20-11.el8.x86_64 : Job spooling tools
Repo        : BaseOS
Matched from:
Provide    : at = 3.1.20-11.el8

[root@rocky8 ~]# dnf -y install at

[root@rocky8 ~]# at
Garbled time

[root@rocky8 ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
[root@rocky8 ~]# systemctl enable --now atd
[root@rocky8 ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-10-31 20:49:10 CST; 1s ago
 Main PID: 26480 (atd)
    Tasks: 1 (limit: 4783)
   Memory: 364.0K
   CGroup: /system.slice/atd.service
           └─26480 /usr/sbin/atd -f

Oct 31 20:49:10 rocky8 systemd[1]: Started Job spooling tools.


[root@rocky8 ~]# rpm -ql at
/etc/at.deny
/etc/pam.d/atd
/etc/sysconfig/atd
/usr/bin/at
/usr/bin/atq
/usr/bin/atrm
/usr/bin/batch
/usr/lib/.build-id
/usr/lib/.build-id/02
/usr/lib/.build-id/02/0647ed6e6ce8fe84e8aac1d6897b5c786af578
/usr/lib/.build-id/d3
/usr/lib/.build-id/d3/c5d5906a6757aec0f1f97ead0c4638d927f33b
/usr/lib/systemd/system/atd.service
/usr/sbin/atd
/usr/sbin/atrun
/usr/share/doc/at
/usr/share/doc/at/ChangeLog
/usr/share/doc/at/README
/usr/share/doc/at/timespec
/usr/share/licenses/at
/usr/share/licenses/at/COPYING
/usr/share/licenses/at/Copyright
/usr/share/man/man1/at.1.gz
/usr/share/man/man1/atq.1.gz
/usr/share/man/man1/atrm.1.gz
/usr/share/man/man1/batch.1.gz
/usr/share/man/man5/at.allow.5.gz
/usr/share/man/man5/at.deny.5.gz
/usr/share/man/man8/atd.8.gz
/usr/share/man/man8/atrun.8.gz
/var/spool/at
/var/spool/at/.SEQ
/var/spool/at/spool

[root@rocky8 ~]# at -h
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
       at [-V] [-q x] [-f file] [-mMlbv] -t time
       at -c job ...
       atq [-V] [-q x]
       at [ -rd ] job ...
       atrm [-V] job ...
       batch

[root@rocky8 ~]# dnf -y install postfix
[root@rocky8 ~]# systemctl enable --now postfix
[root@rocky8 ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-11-01 15:32:09 CST; 1s ago
  Process: 10605 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 10603 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 10599 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 10674 (master)
    Tasks: 3 (limit: 4783)
   Memory: 5.0M
   CGroup: /system.slice/postfix.service
           ├─10674 /usr/libexec/postfix/master -w
           ├─10675 pickup -l -t unix -u
           └─10676 qmgr -l -t unix -u

Nov 01 15:32:08 rocky8 systemd[1]: Starting Postfix Mail Transport Agent...
Nov 01 15:32:09 rocky8 postfix/master[10674]: daemon started -- version 3.5.8, configuration /etc/postfix
Nov 01 15:32:09 rocky8 systemd[1]: Started Postfix Mail Transport Agent.

[root@rocky8 ~]# mail
-bash: mail: command not found
[root@rocky8 ~]# dnf provides mail
Last metadata expiration check: 0:04:03 ago on Mon 01 Nov 2021 03:29:21 PM CST.
mailx-12.5-29.el8.x86_64 : Enhanced implementation of the mailx command
Repo        : BaseOS
Matched from:
Filename    : /bin/mail

[root@rocky8 ~]# dnf -y install mailx

[root@rocky8 ~]# ss -ntl
State           Recv-Q          Send-Q                     Local Address:Port                      Peer Address:Port          Process          
LISTEN          0               128                              0.0.0.0:22                             0.0.0.0:*                              
LISTEN          0               100                            127.0.0.1:25                             0.0.0.0:*                              
LISTEN          0               128                                    *:9090                                 *:*                              
LISTEN          0               70                                     *:33060                                *:*                              
LISTEN          0               128                                    *:3306                                 *:*                              
LISTEN          0               128                                 [::]:22                                [::]:*                              
LISTEN          0               100                                [::1]:25                                [::]:*

[root@rocky8 ~]# at 16:00
warning: commands will be executed using /bin/sh
at> touch /data/a.txt  
at> <EOT>
job 1 at Mon Nov  1 16:00:00 2021
#ctrl+d  保存计划任务

[root@rocky8 ~]# at -l #at -l 查看计划任务
1	Mon Nov  1 16:00:00 2021 a root

[root@rocky8 ~]# at -c 1 #at -c 作业编号 查看计划任务
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
LS_COLORS=rs=0:di=01\;34:ln=01\;36:mh=00:pi=40\;33:so=01\;35:do=01\;35:bd=40\;33\;01:cd=40\;33\;01:or=40\;31\;01:mi=01\;05\;37\;41:su=37\;41:sg=30\;43:ca=30\;41:tw=30\;42:ow=34\;42:st=37\;44:ex=01\;32:\*.tar=01\;31:\*.tgz=01\;31:\*.arc=01\;31:\*.arj=01\;31:\*.taz=01\;31:\*.lha=01\;31:\*.lz4=01\;31:\*.lzh=01\;31:\*.lzma=01\;31:\*.tlz=01\;31:\*.txz=01\;31:\*.tzo=01\;31:\*.t7z=01\;31:\*.zip=01\;31:\*.z=01\;31:\*.dz=01\;31:\*.gz=01\;31:\*.lrz=01\;31:\*.lz=01\;31:\*.lzo=01\;31:\*.xz=01\;31:\*.zst=01\;31:\*.tzst=01\;31:\*.bz2=01\;31:\*.bz=01\;31:\*.tbz=01\;31:\*.tbz2=01\;31:\*.tz=01\;31:\*.deb=01\;31:\*.rpm=01\;31:\*.jar=01\;31:\*.war=01\;31:\*.ear=01\;31:\*.sar=01\;31:\*.rar=01\;31:\*.alz=01\;31:\*.ace=01\;31:\*.zoo=01\;31:\*.cpio=01\;31:\*.7z=01\;31:\*.rz=01\;31:\*.cab=01\;31:\*.wim=01\;31:\*.swm=01\;31:\*.dwm=01\;31:\*.esd=01\;31:\*.jpg=01\;35:\*.jpeg=01\;35:\*.mjpg=01\;35:\*.mjpeg=01\;35:\*.gif=01\;35:\*.bmp=01\;35:\*.pbm=01\;35:\*.pgm=01\;35:\*.ppm=01\;35:\*.tga=01\;35:\*.xbm=01\;35:\*.xpm=01\;35:\*.tif=01\;35:\*.tiff=01\;35:\*.png=01\;35:\*.svg=01\;35:\*.svgz=01\;35:\*.mng=01\;35:\*.pcx=01\;35:\*.mov=01\;35:\*.mpg=01\;35:\*.mpeg=01\;35:\*.m2v=01\;35:\*.mkv=01\;35:\*.webm=01\;35:\*.ogm=01\;35:\*.mp4=01\;35:\*.m4v=01\;35:\*.mp4v=01\;35:\*.vob=01\;35:\*.qt=01\;35:\*.nuv=01\;35:\*.wmv=01\;35:\*.asf=01\;35:\*.rm=01\;35:\*.rmvb=01\;35:\*.flc=01\;35:\*.avi=01\;35:\*.fli=01\;35:\*.flv=01\;35:\*.gl=01\;35:\*.dl=01\;35:\*.xcf=01\;35:\*.xwd=01\;35:\*.yuv=01\;35:\*.cgm=01\;35:\*.emf=01\;35:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=01\;36:\*.au=01\;36:\*.flac=01\;36:\*.m4a=01\;36:\*.mid=01\;36:\*.midi=01\;36:\*.mka=01\;36:\*.mp3=01\;36:\*.mpc=01\;36:\*.ogg=01\;36:\*.ra=01\;36:\*.wav=01\;36:\*.oga=01\;36:\*.opus=01\;36:\*.spx=01\;36:\*.xspf=01\;36:; export LS_COLORS
SSH_CONNECTION=172.31.0.1\ 53218\ 172.31.1.8\ 22; export SSH_CONNECTION
LANG=en_US.UTF-8; export LANG
HISTCONTROL=ignoredups; export HISTCONTROL
HISTTIMEFORMAT=%F\ %T\ ; export HISTTIMEFORMAT
HOSTNAME=rocky8; export HOSTNAME
EDITOR=vim; export EDITOR
XDG_SESSION_ID=1; export XDG_SESSION_ID
USER=root; export USER
PWD=/root; export PWD
HOME=/root; export HOME
SSH_CLIENT=172.31.0.1\ 53218\ 22; export SSH_CLIENT
SSH_TTY=/dev/pts/0; export SSH_TTY
MAIL=/var/spool/mail/root; export MAIL
SHELL=/bin/bash; export SHELL
SHLVL=1; export SHLVL
LOGNAME=root; export LOGNAME
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus; export DBUS_SESSION_BUS_ADDRESS
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH
HISTSIZE=1000; export HISTSIZE
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
cd /root || {
	 echo 'Execution directory inaccessible' >&2
	 exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER169bcccc'
touch /data/a.txt
marcinDELIMITER169bcccc

[root@rocky8 ~]# ls /data/a.txt 
/data/a.txt

[root@rocky8 ~]# stat /data/a.txt 
  File: /data/a.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d	Inode: 131         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-11-01 16:00:00.030963124 +0800
Modify: 2021-11-01 16:00:00.030963124 +0800
Change: 2021-11-01 16:00:00.030963124 +0800
 Birth: -

[root@rocky8 ~]# at now+1minutes
warning: commands will be executed using /bin/sh
at> echo $PATH
at> echo hello
at> hostname
at> <EOT>
job 2 at Mon Nov  1 16:03:00 2021

root@rocky8 ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Mon Nov  1 16:03  16/545   "Output from your job        2"
& 1
Message  1:
From root@rocky8.localdomain  Mon Nov  1 16:03:07 2021
Return-Path: <root@rocky8.localdomain>
X-Original-To: root
Delivered-To: root@rocky8.localdomain
Subject: Output from your job        2
To: root@rocky8.localdomain
Date: Mon,  1 Nov 2021 16:03:07 +0800 (CST)
From: root <root@rocky8.localdomain>
Status: R

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
hello
rocky8

[root@rocky8 ~]# echo hostname | at now+1min
warning: commands will be executed using /bin/sh
job 3 at Mon Nov  1 16:05:00 2021
[root@rocky8 ~]# at -l
3	Mon Nov  1 16:05:00 2021 a root
[root@rocky8 ~]# at -c 3
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
LS_COLORS=rs=0:di=01\;34:ln=01\;36:mh=00:pi=40\;33:so=01\;35:do=01\;35:bd=40\;33\;01:cd=40\;33\;01:or=40\;31\;01:mi=01\;05\;37\;41:su=37\;41:sg=30\;43:ca=30\;41:tw=30\;42:ow=34\;42:st=37\;44:ex=01\;32:\*.tar=01\;31:\*.tgz=01\;31:\*.arc=01\;31:\*.arj=01\;31:\*.taz=01\;31:\*.lha=01\;31:\*.lz4=01\;31:\*.lzh=01\;31:\*.lzma=01\;31:\*.tlz=01\;31:\*.txz=01\;31:\*.tzo=01\;31:\*.t7z=01\;31:\*.zip=01\;31:\*.z=01\;31:\*.dz=01\;31:\*.gz=01\;31:\*.lrz=01\;31:\*.lz=01\;31:\*.lzo=01\;31:\*.xz=01\;31:\*.zst=01\;31:\*.tzst=01\;31:\*.bz2=01\;31:\*.bz=01\;31:\*.tbz=01\;31:\*.tbz2=01\;31:\*.tz=01\;31:\*.deb=01\;31:\*.rpm=01\;31:\*.jar=01\;31:\*.war=01\;31:\*.ear=01\;31:\*.sar=01\;31:\*.rar=01\;31:\*.alz=01\;31:\*.ace=01\;31:\*.zoo=01\;31:\*.cpio=01\;31:\*.7z=01\;31:\*.rz=01\;31:\*.cab=01\;31:\*.wim=01\;31:\*.swm=01\;31:\*.dwm=01\;31:\*.esd=01\;31:\*.jpg=01\;35:\*.jpeg=01\;35:\*.mjpg=01\;35:\*.mjpeg=01\;35:\*.gif=01\;35:\*.bmp=01\;35:\*.pbm=01\;35:\*.pgm=01\;35:\*.ppm=01\;35:\*.tga=01\;35:\*.xbm=01\;35:\*.xpm=01\;35:\*.tif=01\;35:\*.tiff=01\;35:\*.png=01\;35:\*.svg=01\;35:\*.svgz=01\;35:\*.mng=01\;35:\*.pcx=01\;35:\*.mov=01\;35:\*.mpg=01\;35:\*.mpeg=01\;35:\*.m2v=01\;35:\*.mkv=01\;35:\*.webm=01\;35:\*.ogm=01\;35:\*.mp4=01\;35:\*.m4v=01\;35:\*.mp4v=01\;35:\*.vob=01\;35:\*.qt=01\;35:\*.nuv=01\;35:\*.wmv=01\;35:\*.asf=01\;35:\*.rm=01\;35:\*.rmvb=01\;35:\*.flc=01\;35:\*.avi=01\;35:\*.fli=01\;35:\*.flv=01\;35:\*.gl=01\;35:\*.dl=01\;35:\*.xcf=01\;35:\*.xwd=01\;35:\*.yuv=01\;35:\*.cgm=01\;35:\*.emf=01\;35:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=01\;36:\*.au=01\;36:\*.flac=01\;36:\*.m4a=01\;36:\*.mid=01\;36:\*.midi=01\;36:\*.mka=01\;36:\*.mp3=01\;36:\*.mpc=01\;36:\*.ogg=01\;36:\*.ra=01\;36:\*.wav=01\;36:\*.oga=01\;36:\*.opus=01\;36:\*.spx=01\;36:\*.xspf=01\;36:; export LS_COLORS
SSH_CONNECTION=172.31.0.1\ 53218\ 172.31.1.8\ 22; export SSH_CONNECTION
LANG=en_US.UTF-8; export LANG
HISTCONTROL=ignoredups; export HISTCONTROL
HISTTIMEFORMAT=%F\ %T\ ; export HISTTIMEFORMAT
HOSTNAME=rocky8; export HOSTNAME
EDITOR=vim; export EDITOR
XDG_SESSION_ID=1; export XDG_SESSION_ID
USER=root; export USER
PWD=/root; export PWD
HOME=/root; export HOME
SSH_CLIENT=172.31.0.1\ 53218\ 22; export SSH_CLIENT
SSH_TTY=/dev/pts/0; export SSH_TTY
MAIL=/var/spool/mail/root; export MAIL
SHELL=/bin/bash; export SHELL
SHLVL=1; export SHLVL
LOGNAME=root; export LOGNAME
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus; export DBUS_SESSION_BUS_ADDRESS
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH
HISTSIZE=1000; export HISTSIZE
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
cd /root || {
	 echo 'Execution directory inaccessible' >&2
	 exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER1ce002e6'
hostname
marcinDELIMITER1ce002e6

[root@rocky8 ~]# ll -a /var/spool/at/
total 4
drwx------  3 root root 31 Nov  1 16:05 .
drwxr-xr-x. 9 root root 97 Nov  1 15:31 ..
-rw-------  1 root root  6 Nov  1 16:04 .SEQ
drwx------  2 root root  6 Nov  1 16:05 spool

[root@rocky8 ~]# vim at.txt
wall info

[root@rocky8 ~]# at -f at.txt now+1min
warning: commands will be executed using /bin/sh
job 4 at Mon Nov  1 16:09:00 2021

[root@rocky8 ~]# at -l
4	Mon Nov  1 16:09:00 2021 a root

在这里插入图片描述
到时间屏幕上会看到警告信息

[root@rocky8 ~]# su - raymond
Last login: Sun Oct 31 19:45:13 CST 2021 on pts/2
[raymond@rocky8 ~]$ at now+2min
warning: commands will be executed using /bin/sh
at> ^C[raymond@rocky8 ~]$ exit
logout

#拒绝raymond用户创建at计划任务
[root@rocky8 ~]# vim /etc/at.deny
raymond

[root@rocky8 ~]# su - raymond
Last login: Sun Oct 31 20:57:55 CST 2021 on pts/3
[raymond@rocky8 ~]$ at now+2min
You do not have permission to use at.
[raymond@rocky8 ~]$ exit
logout

#只允许raymond用户创建at计划任务
[root@rocky8 ~]# vim /etc/at.allow
raymond

[root@rocky8 ~]# su - raymond
Last login: Sun Oct 31 20:59:45 CST 2021 on pts/3
[raymond@rocky8 ~]$ at now+2min
warning: commands will be executed using /bin/sh
at> ^C[raymond@rocky8 ~]$ exit
logout

[root@rocky8 ~]# useradd boss
[root@rocky8 ~]# su - boss
[boss@rocky8 ~]$ at now+2min
You do not have permission to use at.
[boss@rocky8 ~]$ exit
logout
#创建了/etc/at.allow文件,只有里边允许的才能创建计划任务

[root@rocky8 ~]# rm -f /etc/at.allow

范例: ubuntu at任务存放路径

root@ubuntu2004:~# ll /var/spool/cron/atjobs/
total 20
drwxrwx--T 2 daemon daemon 4096 Oct 31 12:40 ./
drwxr-xr-x 5 root   root   4096 Aug 24 08:46 ../
-rwx------ 1 root   daemon 2902 Oct 31 12:37 a00001019ffb18*
-rwx------ 1 root   daemon 2895 Oct 31 12:37 a00002019ffb18*
-rw------- 1 daemon daemon    6 Oct 31 12:39 .SEQ

3.2 周期性任务计划 cron

周期性任务计划cron相关的程序包:

cron 依赖于crond服务,确保crond守护处于运行状态:

#CentOS 7 以后版本:
systemctl status crond

#CentOS 6:
service crond status

cron任务分为

计划任务日志:/var/log/cron

范例:

root@rocky8 ~]# rpm -ql cronie
/etc/cron.d
/etc/cron.d/0hourly
/etc/cron.deny
/etc/pam.d/crond
/etc/sysconfig/crond
/lib/systemd/system/crond.service
/usr/bin/cronnext
/usr/bin/crontab
/usr/lib/.build-id
/usr/lib/.build-id/02
/usr/lib/.build-id/02/fc08d45998b2b12ac76b2c6993c2053dae1103
/usr/lib/.build-id/73
/usr/lib/.build-id/73/571ebb01669fc6683ae3c87375d1e79018d3fc
/usr/lib/.build-id/90
/usr/lib/.build-id/90/924a16123825d3f422be80ab8e78c7ef4055f1
/usr/sbin/crond
/usr/share/doc/cronie
/usr/share/doc/cronie/AUTHORS
/usr/share/doc/cronie/ChangeLog
/usr/share/doc/cronie/README
/usr/share/licenses/cronie
/usr/share/licenses/cronie/COPYING
/usr/share/man/man1/cronnext.1.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/man/man8/cron.8.gz
/usr/share/man/man8/crond.8.gz
/var/spool/cron

[root@rocky8 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-10-31 13:03:50 CST; 8h ago
 Main PID: 843 (crond)
    Tasks: 1 (limit: 4783)
   Memory: 964.0K
   CGroup: /system.slice/crond.service
           └─843 /usr/sbin/crond -n

Oct 31 15:01:01 rocky8 CROND[10612]: (root) CMD (run-parts /etc/cron.hourly)
Oct 31 15:19:01 rocky8 anacron[10005]: Job `cron.monthly' started
Oct 31 15:19:01 rocky8 anacron[10005]: Job `cron.monthly' terminated
Oct 31 15:19:01 rocky8 anacron[10005]: Normal exit (3 jobs run)
Oct 31 16:01:01 rocky8 CROND[10694]: (root) CMD (run-parts /etc/cron.hourly)
Oct 31 17:01:01 rocky8 CROND[11521]: (root) CMD (run-parts /etc/cron.hourly)
Oct 31 18:01:01 rocky8 CROND[16118]: (root) CMD (run-parts /etc/cron.hourly)
Oct 31 19:01:01 rocky8 CROND[17325]: (root) CMD (run-parts /etc/cron.hourly)
Oct 31 20:01:01 rocky8 CROND[22535]: (root) CMD (run-parts /etc/cron.hourly)
Oct 31 21:01:01 rocky8 CROND[27058]: (root) CMD (run-parts /etc/cron.hourly)

3.2.1 系统cron计划任务

/etc/crontab 格式说明,详情参见 man 5 crontab

注释行以 # 开头

[root@rocky8 ~]# cat /etc/crontab
SHELL=/bin/bash #默认的SHELL类型
PATH=/sbin:/bin:/usr/sbin:/usr/bin #默认的PATH变量值,可修改为其它路径
MAILTO=root #默认标准输出和错误发邮件给root,可以指向其它用户

# 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

root@ubuntu2004:~# cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# 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
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

root@ubuntu2004:~# which run-parts
/usr/bin/run-parts
root@ubuntu2004:~# file `which run-parts`
/usr/bin/run-parts: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=6017e923e9756db667c617f5a9fc12cb5276d74a, for GNU/Linux 3.2.0, stripped

[root@rocky8 ~]# file `which run-parts`
/usr/bin/run-parts: Bourne-Again shell script, ASCII text executable

[root@rocky8 opt]# vim f1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-31
#FileName:      f1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo f1.sh

[root@rocky8 opt]# vim f2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-31
#FileName:      f2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo f2.sh

[root@rocky8 opt]# cd
[root@rocky8 ~]# chmod +x /opt/*
[root@rocky8 ~]# ll /opt/
total 8
-rwxr-xr-x 1 root root 426 Oct 31 21:29 f1.sh
-rwxr-xr-x 1 root root 425 Oct 31 21:29 f2.sh

[root@rocky8 ~]# run-parts  /opt
/opt/f1.sh:

f1.sh
/opt/f2.sh:

f2.sh

[root@rocky8 ~]# crontab -e
* 21 * * 0 echo $PATH 


[root@rocky8 ~]# crontab -e
30 2 1-10 * 6,0 echo $PATH
#这个命令在2点30,或1-10号,或周六/周日执行

计划任务时间表示法:

(1) 特定值
    给定时间点有效取值范围内的值
(2) *
    给定时间点上有效取值范围内的所有值
    表示“每...”
(3) 离散取值
    #,#,#
(4) 连续取值
    #-#
(5) 在指定时间范围上,定义步长
    /#: #即为步长

(6) 特定关健字
@yearly     0 0 1 1 *
@annually   0 0 1 1 *
@monthly    0 0 1 * *
@weekly     0 0 * * 0
@daily      0 0 * * *
@hourly     0 * * * *
@reboot     Run once after reboot

范例:

#晚上9点10分运行echo命令
10 21 * * * raymond /bin/echo "Howdy!"

#每3小时echo和wall命令
0 */3 * * * raymond /bin/echo “howdy”; wall “welcome to Linux!”

crond任务相关文件:

/etc/crontab 配置文件
/etc/cron.d/ 配置文件
/etc/cron.hourly/ 脚本
/etc/cron.daily/ 脚本
/etc/cron.weekly/ 脚本
/etc/cron.monthly/ 脚本

3.2.2 用户计划任务

crontab命令:

crontab命令格式:

crontab [-u user] [-l | -r | -e] [-i]

常用选项:

-l      列出所有任务
-e      编辑任务
-r      移除所有任务
-i      同-r一同使用,以交互式模式移除指定任务
-u user 仅root可运行,指定用户管理cron任务

控制用户执行计划任务:

/etc/cron.{allow,deny}

范例:每个月日期和星期几字段的关系

[root@rocky8 ~]# crontab -e
30 2 1-10 * 6,0 echo $PATH
#这个命令在2点30,或1-10号,或周六/周日执行

[root@rocky8 ~]# man 5 crontab
       Note:  The  day  of a command's execution can be specified in the following two fields — 'day of month', and 'day of week'.  If both
       fields are restricted (i.e., do not contain the "*" character), the command will be run when either field matches the current  time.
       For example,
       "30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
       
[root@rocky8 ~]# crontab -e
30 2 1-10 * * echo $PATH

[root@rocky8 data]# vim test.sh
#!/bin/bash
# 
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-11-01
#FileName:      test.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
[ `date +%w` -eq 0 -o `date +%w` -eq 6 ] && run
#如果表示1-10,并且是周六/周日,在脚本里判断

范例:

[root@rocky8 ~]# crontab -r #删除计划任务

[root@rocky8 ~]# su - raymond
Last login: Sun Oct 31 21:01:14 CST 2021 on pts/3
[raymond@rocky8 ~]$ crontab -e
no crontab for raymond - using an empty one

[raymond@rocky8 ~]$ exit
logout

[root@rocky8 ~]# vim /etc/cron.deny
raymond

[root@rocky8 ~]# su - raymond
Last login: Sun Oct 31 21:41:36 CST 2021 on pts/3
[raymond@rocky8 ~]$ crontab -e
You (raymond) are not allowed to use this program (crontab)
See crontab(1) for more information
[raymond@rocky8 ~]$ exit
logout
#把raymond用户加入/etc/cron.deny,他就不能创建计划任务

root@ubuntu2004:~# cat /etc/rsyslog.d/*.conf
# Log cloudinit generated log messages to file
:syslogtag, isequal, "[CLOUDINIT]" /var/log/cloud-init.log

# comment out the following line to allow CLOUDINIT messages through.
# Doing so means you'll also get CLOUDINIT messages in /var/log/syslog
& stop
#  Default rules for rsyslog.
#
#			For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
#cron.*				/var/log/cron.log #计划任务日志被禁用
#daemon.*			-/var/log/daemon.log
kern.*				-/var/log/kern.log
#lpr.*				-/var/log/lpr.log
mail.*				-/var/log/mail.log
#user.*				-/var/log/user.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
#mail.info			-/var/log/mail.info
#mail.warn			-/var/log/mail.warn
mail.err			/var/log/mail.err

#
# Some "catch-all" log files.
#
#*.=debug;\
#	auth,authpriv.none;\
#	news.none;mail.none	-/var/log/debug
#*.=info;*.=notice;*.=warn;\
#	auth,authpriv.none;\
#	cron,daemon.none;\
#	mail,news.none		-/var/log/messages

#
# Emergencies are sent to everybody logged in.
#
*.emerg				:omusrmsg:*

#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
#	news.=crit;news.=err;news.=notice;\
#	*.=debug;*.=info;\
#	*.=notice;*.=warn	/dev/tty8
# Create an additional socket in postfix's chroot in order not to break
# mail logging when rsyslog is restarted.  If the directory is missing,
# rsyslog will silently skip creating the socket.
$AddUnixListenSocket /var/spool/postfix/dev/log

范例:修改默认的cron的文本编辑工具

root@ubuntu2004:~# crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 

root@ubuntu1804:~# vim .bashrc
export EDITOR=vim

范例:PATH变量

#方法1,在计划任务配置中指定PATH
[root@rocky8 ~]# crontab -l
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
* * * * * useradd hehe;echo $PATH

#方法2,在脚本中指定PATH变量
[root@rocky8 ~]# crontab -l
* * * * * /data/test.sh

[root@rocky8 ~]# cat /data/test.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
useradd hehe

范例:

[root@rocky8 ~]# cd /data
[root@rocky8 data]# vim disk_check.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-31
#FileName:      disk_check.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
WARNING=80
df | sed -En '/^\/dev\/sd/s@^([^ ]+).* ([0-9]+)%.*@\1 \2@p'| while read DEVICE USE;do
    [ $USE -gt $WARNING ] && echo "$DEVICE will be full,USE:$USE" | mail -s diskfull root
done

[root@rocky8 data]# chmod +x disk_check.sh 

[root@rocky8 data]# crontab -e
*/10 * * * * /data/disk_check.sh  

面试题:每年11月每天的6-12点之间每隔2小时执行/app/bin/test.sh

#在6,8,10,12点整共4次分别执行test.sh
[root@rocky8 ~]# crontab -l
0 6-12/2 * 11 * /app/bin/test.sh

#以下配置只会在5,7,9,11点整执行
0 5-12/2 * 11 * /app/bin/test.sh

注意:运行结果的标准输出和错误以邮件通知给相关用户

(1) COMMAND > /dev/null

(2) COMMAND &> /dev/null

cron任务中不建议使用%,它有特殊用途,它表示换行的特殊意义,且第一个%后的所有字符串会被将成当作命令的标准输入

如果在命令中要使用%,则需要用 \ 转义

注意:将%放置于单引号中是不支持的

范例:

30 2 * * * /bin/cp -a /etc/ /data/etc`date +\%F_\%T`
30 2 * * * /bin/cp -a /etc/ /data/etc`date +‘%F_%T’` 有问题

范例:

[root@rocky8 ~]# crontab -e
* * * * * date +%F > /data/date.log

[root@rocky8 ~]# tail -f /var/log/cron 
Nov  2 17:01:01 rocky8 run-parts[1385]: (/etc/cron.hourly) finished 0anacron
Nov  2 17:16:01 rocky8 anacron[1333]: Job `cron.monthly' started
Nov  2 17:16:01 rocky8 anacron[1333]: Job `cron.monthly' terminated
Nov  2 17:16:01 rocky8 anacron[1333]: Normal exit (3 jobs run)
Nov  2 17:21:55 rocky8 crontab[1401]: (root) BEGIN EDIT (root)
Nov  2 17:22:15 rocky8 crontab[9590]: (root) BEGIN EDIT (root)
Nov  2 17:23:14 rocky8 crontab[9590]: (root) REPLACE (root)
Nov  2 17:23:14 rocky8 crontab[9590]: (root) END EDIT (root)
Nov  2 17:24:01 rocky8 CROND[9594]: (root) CMD (date +)  #它把%当换行了,后面的不执行
Nov  2 17:24:10 rocky8 crontab[9597]: (root) LIST (root)

[root@rocky8 ~]# ll /data/date.log
ls: cannot access '/data/date.log': No such file or directory
#文件也没用创建

[root@rocky8 ~]# crontab -e
* * * * * date +\%F > /data/date.log #%前面加转义符\

[root@rocky8 ~]# tail -f /var/log/cron 
...
Nov  2 17:27:01 rocky8 crond[820]: (root) RELOAD (/var/spool/cron/root)
Nov  2 17:27:01 rocky8 CROND[9612]: (root) CMD (date +%F > /data/date.log) #现在就执行正常了

[root@rocky8 ~]# ll /data/date.log
-rw-r--r-- 1 root root 11 Nov  2 17:28 /data/date.log
[root@rocky8 ~]# cat /data/date.log
2021-11-02

[root@rocky8 ~]# rm -f /data/date.log

[root@rocky8 ~]# vim test.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-11-02
#FileName:      test.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
date +%F > /data/date.log

[root@rocky8 ~]# chmod +x test.sh

[root@rocky8 ~]# crontab -e
* * * * * /root/test.sh #也可以把命令写到脚本,直接运行脚本,建议使用此方式

[root@rocky8 ~]# tail -f /var/log/cron 
...
Nov  2 17:54:01 rocky8 crond[820]: (root) RELOAD (/var/spool/cron/root)
Nov  2 17:54:01 rocky8 CROND[9737]: (root) CMD (/root/test.sh)

[root@rocky8 ~]# ll /data/date.log 
-rw-r--r-- 1 root root 11 Nov  2 17:54 /data/date.log

思考:
(1) 如何在秒级别运行任务?

for min in 0 1 2; do echo "hi"; sleep 20; done

范例:

#每10秒执行一次
[root@rocky8 ~]# while true ;do ls -l test.sh ;sleep 10;done
-rwxr-xr-x 1 root root 442 Nov  2 17:52 test.sh
-rwxr-xr-x 1 root root 442 Nov  2 17:52 test.sh
^C

[root@rocky8 ~]# watch -n10 ls -l test.sh #watch -n10  每10秒执行一次查看  
Every 10.0s: ls -l test.sh                                                                                     rocky8: Tue Nov  2 18:10:11 2021

-rwxr-xr-x 1 root root 442 Nov  2 17:52 test.sh

(2) 如何实现每7分钟运行一次任务?

sleep命令:

sleep NUMBER[SUFFIX]...

SUFFIX:
s: 秒, 默认
m: 分
h: 小时
d: 天

范例:

#每7分钟执行一次
[root@rocky8 ~]# while true ;do ls -l test.sh ;sleep 7m;done
-rwxr-xr-x 1 root root 442 Nov  2 17:52 test.sh

3.3 练习

1、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
2、每两小时取出当前系统/proc/meminfo文件中以S或M开头的信息追加至/tmp/meminfo.txt文件中
3、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就执行wall警报

标签:01,第十一章,51,64,usr,进程,172.31,root,rocky8
来源: https://blog.csdn.net/qq_25599925/article/details/121072736