Linux學(xué)習(xí)第八周


一缆八、顯示統(tǒng)計(jì)占用系統(tǒng)內(nèi)存最多的進(jìn)程曲掰,并排序疾捍;

##第一種方式
[root@localhost ~]# ps aux --sort=-%mem | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.5  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.3  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.1  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.2  0.3 161692  6628 ?        Ss   13:17   0:00 sshd: root@pts/0
##第二種方式
[root@localhost ~]# ps aux --sort=-rss | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.4  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.2  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
##第三種方式
[root@localhost ~]# ps aux --sort=-rssize | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.3  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.2  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
##第四種方式
[root@localhost ~]# ps aux --sort=-rsz | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.3  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.1  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
##第五種方式
[root@localhost ~]# ps aux | sort -k4nr | head -5
root       6531  0.3  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root       6215  0.1  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
root      16298  0.0  0.3 161348  6268 ?        Ss   13:17   0:00 sshd: root@notty
##第六種方式
[root@localhost ~]# top    然后敲擊M鍵即可
top - 13:24:21 up 8 min,  1 user,  load average: 0.01, 0.08, 0.06
Tasks: 118 total,   3 running, 115 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863252 total,  1572832 free,    98684 used,   191736 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1584068 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  6531 root      20   0  573820  19152   6024 S   0.0  1.0   0:00.64 tuned
  6215 root      20   0  554064  10944   6840 S   0.0  0.6   0:00.33 NetworkManager
  6223 polkitd   20   0  612228  10080   4676 S   0.0  0.5   0:00.13 polkitd
 16296 root      20   0  161692   6692   5152 S   0.0  0.4   0:00.17 sshd
 16298 root      20   0  161348   6268   4884 S   0.0  0.3   0:00.05 sshd
  6532 root      20   0  216408   4652   3460 S   0.0  0.2   0:00.43 rsyslogd
  6534 root      20   0  112756   4320   3292 S   0.0  0.2   0:00.02 sshd
  6786 postfix   20   0   89716   4092   3088 S   0.0  0.2   0:00.01 qmgr
  6785 postfix   20   0   89648   4060   3064 S   0.0  0.2   0:00.01 pickup

二、編寫腳本蜈缤,使用for和while分別實(shí)現(xiàn)192.168.1.0/24網(wǎng)段內(nèi)拾氓,地址是否能夠ping通,若ping通則輸出"success!"底哥,若ping不通則輸出"fail!"咙鞍;

1、使用for循環(huán)實(shí)現(xiàn)趾徽;
[root@localhost ~]# mkdir /usr/sh
[root@localhost ~]# cd /usr/sh
[root@localhost sh]# cat ip_check_for.sh

#!/bin/sh
NETWORKID=192.168.1.
for HOSTID in {1..254};do
{
        if /bin/ping -c1 -W1 $NETWORKID$HOSTID >/dev/null ;then
                echo "$NETWORKID$HOSTID is success!"
        else
                echo "$NETWORKID$HOSTID is fail!"
        fi
} &   ###并發(fā)執(zhí)行
done
wait
[root@localhost sh]# vim ip_check_for.sh
[root@localhost sh]# sh ip_check_for.sh
192.168.1.1 is success!
192.168.1.111 is success!
192.168.1.2 is fail!
192.168.1.6 is fail!
192.168.1.3 is fail!
192.168.1.5 is fail!
192.168.1.7 is fail!
192.168.1.10 is fail!
192.168.1.16 is fail!
192.168.1.9 is fail!
192.168.1.12 is fail!
192.168.1.17 is fail!
192.168.1.18 is fail!
192.168.1.14 is fail!
192.168.1.20 is fail!
192.168.1.27 is fail!
192.168.1.30 is fail!
192.168.1.23 is fail!
192.168.1.11 is fail!
192.168.1.31 is fail!
192.168.1.24 is fail!
192.168.1.35 is fail!
192.168.1.26 is fail!
192.168.1.39 is fail!
192.168.1.29 is fail!
192.168.1.37 is fail!
192.168.1.32 is fail!
192.168.1.43 is fail!
192.168.1.19 is fail!
192.168.1.21 is fail!
192.168.1.38 is fail!
192.168.1.8 is fail!
192.168.1.44 is fail!
192.168.1.4 is fail!
192.168.1.41 is fail!
192.168.1.13 is fail!
192.168.1.36 is fail!
192.168.1.22 is fail!
192.168.1.47 is fail!
192.168.1.49 is fail!
192.168.1.48 is fail!
192.168.1.15 is fail!
192.168.1.28 is fail!
192.168.1.34 is fail!
192.168.1.25 is fail!
192.168.1.40 is fail!
192.168.1.50 is fail!
192.168.1.33 is fail!
192.168.1.46 is fail!
192.168.1.51 is fail!
192.168.1.54 is fail!
192.168.1.53 is fail!
192.168.1.57 is fail!
192.168.1.59 is fail!
192.168.1.66 is fail!
192.168.1.73 is fail!
192.168.1.76 is fail!
192.168.1.64 is fail!
192.168.1.60 is fail!
192.168.1.67 is fail!
192.168.1.80 is fail!
192.168.1.63 is fail!
192.168.1.61 is fail!
192.168.1.71 is fail!
192.168.1.81 is fail!
192.168.1.56 is fail!
192.168.1.68 is fail!
192.168.1.65 is fail!
192.168.1.88 is fail!
192.168.1.52 is fail!
192.168.1.42 is fail!
192.168.1.90 is fail!
192.168.1.69 is fail!
192.168.1.72 is fail!
192.168.1.55 is fail!
192.168.1.62 is fail!
192.168.1.45 is fail!
192.168.1.84 is fail!
192.168.1.95 is fail!
192.168.1.99 is fail!
192.168.1.77 is fail!
192.168.1.58 is fail!
192.168.1.75 is fail!
192.168.1.79 is fail!
192.168.1.85 is fail!
192.168.1.82 is fail!
192.168.1.70 is fail!
192.168.1.86 is fail!
192.168.1.91 is fail!
192.168.1.93 is fail!
192.168.1.94 is fail!
192.168.1.112 is fail!
192.168.1.83 is fail!
192.168.1.104 is fail!
192.168.1.115 is fail!
192.168.1.96 is fail!
192.168.1.87 is fail!
192.168.1.100 is fail!
192.168.1.78 is fail!
192.168.1.74 is fail!
192.168.1.116 is fail!
192.168.1.106 is fail!
192.168.1.89 is fail!
192.168.1.131 is fail!
192.168.1.119 is fail!
192.168.1.97 is fail!
192.168.1.105 is fail!
192.168.1.101 is fail!
192.168.1.121 is fail!
192.168.1.113 is fail!
192.168.1.103 is fail!
192.168.1.92 is fail!
192.168.1.120 is fail!
192.168.1.98 is fail!
192.168.1.107 is fail!
192.168.1.130 is fail!
192.168.1.122 is fail!
192.168.1.109 is fail!
192.168.1.117 is fail!
192.168.1.114 is fail!
192.168.1.108 is fail!
192.168.1.124 is fail!
192.168.1.118 is fail!
192.168.1.136 is fail!
192.168.1.133 is fail!
192.168.1.127 is fail!
192.168.1.123 is fail!
192.168.1.137 is fail!
192.168.1.134 is fail!
192.168.1.139 is fail!
192.168.1.129 is fail!
192.168.1.102 is fail!
192.168.1.125 is fail!
192.168.1.110 is fail!
192.168.1.140 is fail!
192.168.1.132 is fail!
192.168.1.128 is fail!
192.168.1.147 is fail!
192.168.1.142 is fail!
192.168.1.150 is fail!
192.168.1.143 is fail!
192.168.1.146 is fail!
192.168.1.144 is fail!
192.168.1.141 is fail!
192.168.1.152 is fail!
192.168.1.154 is fail!
192.168.1.145 is fail!
192.168.1.156 is fail!
192.168.1.155 is fail!
192.168.1.135 is fail!
192.168.1.126 is fail!
192.168.1.149 is fail!
192.168.1.158 is fail!
192.168.1.138 is fail!
192.168.1.153 is fail!
192.168.1.148 is fail!
192.168.1.151 is fail!
192.168.1.159 is fail!
192.168.1.157 is fail!
192.168.1.166 is fail!
192.168.1.165 is fail!
192.168.1.186 is fail!
192.168.1.163 is fail!
192.168.1.161 is fail!
192.168.1.188 is fail!
192.168.1.164 is fail!
192.168.1.171 is fail!
192.168.1.189 is fail!
192.168.1.172 is fail!
192.168.1.168 is fail!
192.168.1.167 is fail!
192.168.1.179 is fail!
192.168.1.183 is fail!
192.168.1.170 is fail!
192.168.1.185 is fail!
192.168.1.173 is fail!
192.168.1.177 is fail!
192.168.1.192 is fail!
192.168.1.169 is fail!
192.168.1.180 is fail!
192.168.1.209 is fail!
192.168.1.176 is fail!
192.168.1.181 is fail!
192.168.1.190 is fail!
192.168.1.162 is fail!
192.168.1.178 is fail!
192.168.1.200 is fail!
192.168.1.182 is fail!
192.168.1.174 is fail!
192.168.1.184 is fail!
192.168.1.201 is fail!
192.168.1.187 is fail!
192.168.1.205 is fail!
192.168.1.196 is fail!
192.168.1.191 is fail!
192.168.1.203 is fail!
192.168.1.213 is fail!
192.168.1.160 is fail!
192.168.1.193 is fail!
192.168.1.175 is fail!
192.168.1.195 is fail!
192.168.1.206 is fail!
192.168.1.212 is fail!
192.168.1.204 is fail!
192.168.1.210 is fail!
192.168.1.207 is fail!
192.168.1.199 is fail!
192.168.1.216 is fail!
192.168.1.208 is fail!
192.168.1.194 is fail!
192.168.1.198 is fail!
192.168.1.197 is fail!
192.168.1.223 is fail!
192.168.1.214 is fail!
192.168.1.234 is fail!
192.168.1.225 is fail!
192.168.1.211 is fail!
192.168.1.215 is fail!
192.168.1.247 is fail!
192.168.1.202 is fail!
192.168.1.226 is fail!
192.168.1.217 is fail!
192.168.1.219 is fail!
192.168.1.233 is fail!
192.168.1.237 is fail!
192.168.1.218 is fail!
192.168.1.241 is fail!
192.168.1.220 is fail!
192.168.1.243 is fail!
192.168.1.227 is fail!
192.168.1.229 is fail!
192.168.1.231 is fail!
192.168.1.242 is fail!
192.168.1.244 is fail!
192.168.1.238 is fail!
192.168.1.228 is fail!
192.168.1.235 is fail!
192.168.1.248 is fail!
192.168.1.246 is fail!
192.168.1.232 is fail!
192.168.1.254 is fail!
192.168.1.221 is fail!
192.168.1.230 is fail!
192.168.1.249 is fail!
192.168.1.252 is fail!
192.168.1.224 is fail!
192.168.1.222 is fail!
192.168.1.245 is fail!
192.168.1.240 is fail!
192.168.1.251 is fail!
192.168.1.250 is fail!
192.168.1.253 is fail!
192.168.1.236 is fail!
192.168.1.239 is fail!
2续滋、使用while循環(huán)實(shí)現(xiàn);
##生成要掃描的IP列表
[root@localhost sh]# echo 192.168.1.{1..254} | tr -s " " "\n" > /usr/sh/iplist.txt
[root@localhost sh]# cat iplist.txt
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20
192.168.1.21
192.168.1.22
192.168.1.23
192.168.1.24
192.168.1.25
192.168.1.26
192.168.1.27
192.168.1.28
192.168.1.29
192.168.1.30
192.168.1.31
192.168.1.32
192.168.1.33
192.168.1.34
192.168.1.35
192.168.1.36
192.168.1.37
192.168.1.38
192.168.1.39
192.168.1.40
192.168.1.41
192.168.1.42
192.168.1.43
192.168.1.44
192.168.1.45
192.168.1.46
192.168.1.47
192.168.1.48
192.168.1.49
192.168.1.50
192.168.1.51
192.168.1.52
192.168.1.53
192.168.1.54
192.168.1.55
192.168.1.56
192.168.1.57
192.168.1.58
192.168.1.59
192.168.1.60
192.168.1.61
192.168.1.62
192.168.1.63
192.168.1.64
192.168.1.65
192.168.1.66
192.168.1.67
192.168.1.68
192.168.1.69
192.168.1.70
192.168.1.71
192.168.1.72
192.168.1.73
192.168.1.74
192.168.1.75
192.168.1.76
192.168.1.77
192.168.1.78
192.168.1.79
192.168.1.80
192.168.1.81
192.168.1.82
192.168.1.83
192.168.1.84
192.168.1.85
192.168.1.86
192.168.1.87
192.168.1.88
192.168.1.89
192.168.1.90
192.168.1.91
192.168.1.92
192.168.1.93
192.168.1.94
192.168.1.95
192.168.1.96
192.168.1.97
192.168.1.98
192.168.1.99
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
192.168.1.105
192.168.1.106
192.168.1.107
192.168.1.108
192.168.1.109
192.168.1.110
192.168.1.111
192.168.1.112
192.168.1.113
192.168.1.114
192.168.1.115
192.168.1.116
192.168.1.117
192.168.1.118
192.168.1.119
192.168.1.120
192.168.1.121
192.168.1.122
192.168.1.123
192.168.1.124
192.168.1.125
192.168.1.126
192.168.1.127
192.168.1.128
192.168.1.129
192.168.1.130
192.168.1.131
192.168.1.132
192.168.1.133
192.168.1.134
192.168.1.135
192.168.1.136
192.168.1.137
192.168.1.138
192.168.1.139
192.168.1.140
192.168.1.141
192.168.1.142
192.168.1.143
192.168.1.144
192.168.1.145
192.168.1.146
192.168.1.147
192.168.1.148
192.168.1.149
192.168.1.150
192.168.1.151
192.168.1.152
192.168.1.153
192.168.1.154
192.168.1.155
192.168.1.156
192.168.1.157
192.168.1.158
192.168.1.159
192.168.1.160
192.168.1.161
192.168.1.162
192.168.1.163
192.168.1.164
192.168.1.165
192.168.1.166
192.168.1.167
192.168.1.168
192.168.1.169
192.168.1.170
192.168.1.171
192.168.1.172
192.168.1.173
192.168.1.174
192.168.1.175
192.168.1.176
192.168.1.177
192.168.1.178
192.168.1.179
192.168.1.180
192.168.1.181
192.168.1.182
192.168.1.183
192.168.1.184
192.168.1.185
192.168.1.186
192.168.1.187
192.168.1.188
192.168.1.189
192.168.1.190
192.168.1.191
192.168.1.192
192.168.1.193
192.168.1.194
192.168.1.195
192.168.1.196
192.168.1.197
192.168.1.198
192.168.1.199
192.168.1.200
192.168.1.201
192.168.1.202
192.168.1.203
192.168.1.204
192.168.1.205
192.168.1.206
192.168.1.207
192.168.1.208
192.168.1.209
192.168.1.210
192.168.1.211
192.168.1.212
192.168.1.213
192.168.1.214
192.168.1.215
192.168.1.216
192.168.1.217
192.168.1.218
192.168.1.219
192.168.1.220
192.168.1.221
192.168.1.222
192.168.1.223
192.168.1.224
192.168.1.225
192.168.1.226
192.168.1.227
192.168.1.228
192.168.1.229
192.168.1.230
192.168.1.231
192.168.1.232
192.168.1.233
192.168.1.234
192.168.1.235
192.168.1.236
192.168.1.237
192.168.1.238
192.168.1.239
192.168.1.240
192.168.1.241
192.168.1.242
192.168.1.243
192.168.1.244
192.168.1.245
192.168.1.246
192.168.1.247
192.168.1.248
192.168.1.249
192.168.1.250
192.168.1.251
192.168.1.252
192.168.1.253
192.168.1.254

[root@localhost sh]# chmod +x ip_check_while.sh
[root@localhost sh]# sh ip_check_while.sh
192.168.1.1 is success!
192.168.1.111 is success!
192.168.1.20 is fail!
192.168.1.8 is fail!
192.168.1.23 is fail!
192.168.1.9 is fail!
192.168.1.11 is fail!
192.168.1.10 is fail!
192.168.1.12 is fail!
192.168.1.14 is fail!
192.168.1.13 is fail!
192.168.1.21 is fail!
192.168.1.7 is fail!
192.168.1.17 is fail!
192.168.1.22 is fail!
192.168.1.2 is fail!
192.168.1.5 is fail!
192.168.1.28 is fail!
192.168.1.3 is fail!
192.168.1.4 is fail!
192.168.1.29 is fail!
192.168.1.19 is fail!
192.168.1.66 is fail!
192.168.1.27 is fail!
192.168.1.39 is fail!
192.168.1.32 is fail!
192.168.1.25 is fail!
192.168.1.40 is fail!
192.168.1.16 is fail!
192.168.1.31 is fail!
192.168.1.68 is fail!
192.168.1.33 is fail!
192.168.1.15 is fail!
192.168.1.43 is fail!
192.168.1.58 is fail!
192.168.1.62 is fail!
192.168.1.35 is fail!
192.168.1.53 is fail!
192.168.1.44 is fail!
192.168.1.36 is fail!
192.168.1.54 is fail!
192.168.1.45 is fail!
192.168.1.37 is fail!
192.168.1.47 is fail!
192.168.1.67 is fail!
192.168.1.46 is fail!
192.168.1.18 is fail!
192.168.1.63 is fail!
192.168.1.30 is fail!
192.168.1.24 is fail!
192.168.1.42 is fail!
192.168.1.38 is fail!
192.168.1.80 is fail!
192.168.1.60 is fail!
192.168.1.52 is fail!
192.168.1.41 is fail!
192.168.1.76 is fail!
192.168.1.56 is fail!
192.168.1.49 is fail!
192.168.1.51 is fail!
192.168.1.48 is fail!
192.168.1.72 is fail!
192.168.1.70 is fail!
192.168.1.59 is fail!
192.168.1.75 is fail!
192.168.1.78 is fail!
192.168.1.6 is fail!
192.168.1.61 is fail!
192.168.1.77 is fail!
192.168.1.65 is fail!
192.168.1.73 is fail!
192.168.1.74 is fail!
192.168.1.79 is fail!
192.168.1.83 is fail!
192.168.1.55 is fail!
192.168.1.64 is fail!
192.168.1.85 is fail!
192.168.1.34 is fail!
192.168.1.71 is fail!
192.168.1.81 is fail!
192.168.1.69 is fail!
192.168.1.84 is fail!
192.168.1.57 is fail!
192.168.1.50 is fail!
192.168.1.82 is fail!
192.168.1.87 is fail!
192.168.1.26 is fail!
192.168.1.88 is fail!
192.168.1.91 is fail!
192.168.1.92 is fail!
192.168.1.86 is fail!
192.168.1.93 is fail!
192.168.1.89 is fail!
192.168.1.95 is fail!
192.168.1.94 is fail!
192.168.1.98 is fail!
192.168.1.90 is fail!
192.168.1.96 is fail!
192.168.1.102 is fail!
192.168.1.99 is fail!
192.168.1.104 is fail!
192.168.1.105 is fail!
192.168.1.103 is fail!
192.168.1.97 is fail!
192.168.1.100 is fail!
192.168.1.108 is fail!
192.168.1.110 is fail!
192.168.1.101 is fail!
192.168.1.107 is fail!
192.168.1.106 is fail!
192.168.1.112 is fail!
192.168.1.113 is fail!
192.168.1.109 is fail!
192.168.1.115 is fail!
192.168.1.119 is fail!
192.168.1.114 is fail!
192.168.1.116 is fail!
192.168.1.117 is fail!
192.168.1.122 is fail!
192.168.1.118 is fail!
192.168.1.121 is fail!
192.168.1.123 is fail!
192.168.1.126 is fail!
192.168.1.120 is fail!
192.168.1.129 is fail!
192.168.1.127 is fail!
192.168.1.124 is fail!
192.168.1.130 is fail!
192.168.1.128 is fail!
192.168.1.131 is fail!
192.168.1.133 is fail!
192.168.1.125 is fail!
192.168.1.132 is fail!
192.168.1.135 is fail!
192.168.1.134 is fail!
192.168.1.136 is fail!
192.168.1.141 is fail!
192.168.1.139 is fail!
192.168.1.140 is fail!
192.168.1.138 is fail!
192.168.1.142 is fail!
192.168.1.137 is fail!
192.168.1.145 is fail!
192.168.1.143 is fail!
192.168.1.146 is fail!
192.168.1.149 is fail!
192.168.1.144 is fail!
192.168.1.148 is fail!
192.168.1.150 is fail!
192.168.1.154 is fail!
192.168.1.151 is fail!
192.168.1.155 is fail!
192.168.1.147 is fail!
192.168.1.153 is fail!
192.168.1.156 is fail!
192.168.1.160 is fail!
192.168.1.152 is fail!
192.168.1.157 is fail!
192.168.1.158 is fail!
192.168.1.163 is fail!
192.168.1.161 is fail!
192.168.1.164 is fail!
192.168.1.169 is fail!
192.168.1.159 is fail!
192.168.1.168 is fail!
192.168.1.167 is fail!
192.168.1.166 is fail!
192.168.1.162 is fail!
192.168.1.165 is fail!
192.168.1.172 is fail!
192.168.1.174 is fail!
192.168.1.175 is fail!
192.168.1.173 is fail!
192.168.1.177 is fail!
192.168.1.171 is fail!
192.168.1.176 is fail!
192.168.1.178 is fail!
192.168.1.179 is fail!
192.168.1.170 is fail!
192.168.1.181 is fail!
192.168.1.180 is fail!
192.168.1.183 is fail!
192.168.1.184 is fail!
192.168.1.182 is fail!
192.168.1.185 is fail!
192.168.1.186 is fail!
192.168.1.189 is fail!
192.168.1.192 is fail!
192.168.1.188 is fail!
192.168.1.191 is fail!
192.168.1.190 is fail!
192.168.1.187 is fail!
192.168.1.194 is fail!
192.168.1.193 is fail!
192.168.1.195 is fail!
192.168.1.199 is fail!
192.168.1.198 is fail!
192.168.1.196 is fail!
192.168.1.201 is fail!
192.168.1.204 is fail!
192.168.1.200 is fail!
192.168.1.205 is fail!
192.168.1.203 is fail!
192.168.1.197 is fail!
192.168.1.207 is fail!
192.168.1.202 is fail!
192.168.1.209 is fail!
192.168.1.206 is fail!
192.168.1.208 is fail!
192.168.1.211 is fail!
192.168.1.213 is fail!
192.168.1.212 is fail!
192.168.1.216 is fail!
192.168.1.214 is fail!
192.168.1.217 is fail!
192.168.1.210 is fail!
192.168.1.222 is fail!
192.168.1.219 is fail!
192.168.1.215 is fail!
192.168.1.218 is fail!
192.168.1.223 is fail!
192.168.1.221 is fail!
192.168.1.220 is fail!
192.168.1.224 is fail!
192.168.1.228 is fail!
192.168.1.225 is fail!
192.168.1.226 is fail!
192.168.1.227 is fail!
192.168.1.231 is fail!
192.168.1.229 is fail!
192.168.1.232 is fail!
192.168.1.230 is fail!
192.168.1.234 is fail!
192.168.1.238 is fail!
192.168.1.233 is fail!
192.168.1.239 is fail!
192.168.1.235 is fail!
192.168.1.236 is fail!
192.168.1.241 is fail!
192.168.1.242 is fail!
192.168.1.237 is fail!
192.168.1.245 is fail!
192.168.1.240 is fail!
192.168.1.246 is fail!
192.168.1.243 is fail!
192.168.1.244 is fail!
192.168.1.247 is fail!
192.168.1.248 is fail!
192.168.1.250 is fail!
192.168.1.251 is fail!
192.168.1.249 is fail!
192.168.1.252 is fail!
192.168.1.254 is fail!
192.168.1.253 is fail!

三孵奶、每周的工作日1:30疲酌,將/etc備份至/backup目錄中,保存的文件名稱格式 為“etcbak-yyyy-mm-dd-HH.tar.xz”了袁,其中日期是前一天的時(shí)間朗恳;

1、編寫腳本和定時(shí)計(jì)劃载绿;
##創(chuàng)建腳本
[root@localhost sh]# pwd
/usr/sh
[root@localhost sh]# cat etc_backup.sh
#!/bin/bash
DATE_TIME=`date -d "-1 day" +%F-%H`
tar -Jcf /backup/etcbak-$DATE_TIME.tar.xz /etc/ > /dev/null

##腳本添加執(zhí)行權(quán)限
[root@localhost sh]# chmod +x etc_backup.sh

##添加定時(shí)任務(wù)
[root@localhost sh]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
30 1 * * 1-5 root sh /usr/sh/etc_backup.sh &> /dev/null

--

測(cè)試:

1粥诫、確認(rèn)腳本沒(méi)被執(zhí)行過(guò);

[root@localhost sh]# date
2020年 07月 30日 星期四 16:57:53 CST
[root@localhost sh]# ll /backup
總用量 0

2崭庸、修改系統(tǒng)時(shí)間觸發(fā)執(zhí)行條件怀浆;

[root@localhost sh]# date -s "2020-07-31 01:29:50"
2020年 07月 31日 星期五 01:29:50 CST
[root@localhost sh]# ll /backup
總用量 192
-rw-r--r-- 1 root root 180224 7月  31 01:30 etcbak-2020-07-30-01.tar.xz

四、工作日時(shí)間怕享,每10分鐘執(zhí)行一次磁盤空間檢查执赡,一旦發(fā)現(xiàn)任何分區(qū)利用率高 于80%,就發(fā)送郵件報(bào)警函筋。


[root@localhost ~]# cd /usr/sh  #沒(méi)有此目錄則創(chuàng)建或者在自己想創(chuàng)建的目錄也可以放腳本
[root@localhost sh]# cat disk_check.sh
#!/bin/bash
#取分區(qū)使用率的最大值
#sed方法
#Disk_Used=`df -h | sed -nr "/\/dev\/sda/s#.* +([0-9]+)% .*#\1#p" |sort -nr |head -1`

#awk方法
Disk_Used=`df -h | awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}'`

#取使用率最大分區(qū)名
Partition_Name=`df -h | grep "$Disk_Used%" |cut -d" " -f1`

if [ $Disk_Used -gt 80 ];then
        echo -e "Warning!! \n $Partition_Name's used space is more than 80%" | mail -s Warning root
fi

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0

[root@localhost ~]# df -h | sed -nr "/\/dev\/sda/s#.* +([0-9]+)% .*#\1#p" |sort -nr |head -1
14

[root@localhost ~]# df -h | awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}'
14

[root@localhost ~]# df -h | grep "14%" |cut -d" " -f1
/dev/sda1


[root@localhost sh]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
*/10 * * * 1-5 root sh /usr/sh/disk_check.sh &>/dev/null
2沙合、測(cè)試;
1)選擇測(cè)試分區(qū)
[root@localhost sh]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0

此處我們?cè)?boot分區(qū)進(jìn)行測(cè)試跌帐。

2)將測(cè)試文件寫入/boot分區(qū)中灌诅;

[root@localhost sh]# dd if=/dev/zero of=/boot/test.txt bs=1M count=680
680+0 records in
680+0 records out
713031680 bytes (713 MB) copied, 14.1995 s, 50.2 MB/s

[root@localhost sh]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  813M  202M  81% /boot
tmpfs                     98M     0   98M   0% /run/user/0
3)更改定時(shí)計(jì)劃任務(wù)為每天每分鐘執(zhí)行一次;
[root@localhost sh]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
*/1 * * * * root sh /usr/sh/disk_check.sh &>/dev/null
4)檢查計(jì)劃任務(wù)日志含末,檢查郵件猜拾;
[root@localhost ~]# tail /var/log/cron
Aug  2 00:01:01 localhost run-parts(/etc/cron.hourly)[6846]: starting 0anacron
Aug  2 00:01:01 localhost anacron[6855]: Anacron started on 2020-08-02
Aug  2 00:01:02 localhost anacron[6855]: Will run job `cron.daily' in 33 min.
Aug  2 00:01:02 localhost anacron[6855]: Will run job `cron.weekly' in 53 min.
Aug  2 00:01:02 localhost anacron[6855]: Will run job `cron.monthly' in 73 min.
Aug  2 00:01:02 localhost anacron[6855]: Jobs will be executed sequentially
Aug  2 00:01:02 localhost run-parts(/etc/cron.hourly)[6857]: finished 0anacron
Aug  1 16:08:01 localhost crond[6197]: (*system*) RELOAD (/etc/crontab)
Aug  1 16:08:01 localhost CROND[6933]: (root) CMD (sh /usr/sh/disk_check.sh &>/dev/null)
Aug  1 16:09:01 localhost CROND[6953]: (root) CMD (sh /usr/sh/disk_check.sh &>/dev/null)

[root@localhost mail]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Sat Aug  1 16:08  19/654   "Warning"
&
#####或者直接查看root的郵件
[root@localhost mail]# cat /var/spool/mail/root
From root@localhost.localdomain  Sat Aug  1 16:08:01 2020
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
        id 86DADAEEF0; Sat,  1 Aug 2020 16:08:01 +0800 (CST)
Date: Sat, 01 Aug 2020 16:08:01 +0800
To: root@localhost.localdomain
Subject: Warning
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20200801080801.86DADAEEF0@localhost.localdomain>
From: root@localhost.localdomain (root)

Warning!!
 /dev/sda1's used space is more than 80%
經(jīng)測(cè)試磁盤空間利用率檢查成功,然后將定時(shí)任務(wù)時(shí)間修改回去佣盒,測(cè)試的文件刪除挎袜,以后其它分區(qū)的檢查對(duì)應(yīng)做修改即可。

!6⒁恰紊搪!時(shí)間一定要同步,方便觀察
同步方式:

[root@localhost ~]# yum install ntpdate -y
[root@localhost ~]# ntpdate -u cn.pool.ntp.org

H啊R!出現(xiàn)報(bào)錯(cuò) -bash: mail: command not found
解決辦法:

root@localhost mail]#  yum -y install mailx

就可以了爸黄。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末滞伟,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子炕贵,更是在濱河造成了極大的恐慌梆奈,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件称开,死亡現(xiàn)場(chǎng)離奇詭異亩钟,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)鳖轰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門清酥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蕴侣,你說(shuō)我怎么就攤上這事总处。” “怎么了睛蛛?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)胧谈。 經(jīng)常有香客問(wèn)我忆肾,道長(zhǎng),這世上最難降的妖魔是什么菱肖? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任客冈,我火速辦了婚禮,結(jié)果婚禮上稳强,老公的妹妹穿的比我還像新娘场仲。我一直安慰自己,他們只是感情好退疫,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布渠缕。 她就那樣靜靜地躺著,像睡著了一般褒繁。 火紅的嫁衣襯著肌膚如雪亦鳞。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音燕差,去河邊找鬼遭笋。 笑死,一個(gè)胖子當(dāng)著我的面吹牛徒探,可吹牛的內(nèi)容都是我干的瓦呼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼测暗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼央串!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起偷溺,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蹋辅,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后挫掏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侦另,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年尉共,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了褒傅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡袄友,死狀恐怖殿托,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情剧蚣,我是刑警寧澤支竹,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站鸠按,受9級(jí)特大地震影響礼搁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜目尖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一馒吴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瑟曲,春花似錦饮戳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至烦衣,卻和暖如春篮赢,著一層夾襖步出監(jiān)牢的瞬間齿椅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工启泣, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涣脚,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓寥茫,卻偏偏與公主長(zhǎng)得像遣蚀,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子纱耻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354