八、shell脚本进阶、系统进程与计划。

1.显示统计占用系统内存最多的进程,并排序.

[root@localhost ~]#ps aux --sort -rss |head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6311  0.0  0.6 573816 14024 ?        Ssl  Sep16   3:16 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6314  0.0  0.4 298268  8856 ?        Ssl  Sep16   1:40 /usr/sbin/rsyslogd -n
root       6057  0.0  0.3 699056  7972 ?        Ssl  Sep16   0:46 /usr/sbin/NetworkManager --no-daemon
root       3016  0.0  0.3  39080  7776 ?        Ss   Sep16   2:03 /usr/lib/systemd/systemd-journald
[root@localhost ~]#ps aux --sort -%mem |head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6311  0.0  0.6 573816 14024 ?        Ssl  Sep16   3:16 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6314  0.0  0.4 298268  8856 ?        Ssl  Sep16   1:40 /usr/sbin/rsyslogd -n
root       6057  0.0  0.3 699056  7972 ?        Ssl  Sep16   0:46 /usr/sbin/NetworkManager --no-daemon
root       3016  0.0  0.3  39080  7776 ?        Ss   Sep16   2:03 /usr/lib/systemd/systemd-journald

2.编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!".

for循环

[root@localhost /data/scripts]#bash !*
bash ip_for.sh
192.168.11.1 is success!
192.168.11.3 is success!
192.168.11.8 is success!
192.168.11.9 is success!
192.168.11.5 is success!
192.168.11.34 is success!
192.168.11.15 is success!
192.168.11.47 is success!
192.168.11.31 is success!
192.168.11.32 is success!
192.168.11.6 is success!
192.168.11.33 is success!
192.168.11.46 is success!
192.168.11.10 is success!
192.168.11.45 is success!
192.168.11.142 is success!
192.168.11.11 is fail!
[root@localhost /data/scripts]#cat !*
cat ip_for.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: ip_for.sh
#********************************************************************
NETID=192.168.11.
for HOSTID in {1..254};do
{
	if /bin/ping -c1 -W1 $NETID$HOSTID >/dev/null ; then
		echo "$NETID$HOSTID is success!"
	else
		echo "$NETID$HOSTID is fail!"
	fi
} &
done
wait

while循环

[root@localhost /data/scripts]#bash ip_while.sh 
192.168.11.3 success!
192.168.11.1 success!
192.168.11.6 success!
192.168.11.5 success!
192.168.11.15 success!
192.168.11.9 success!
192.168.11.8 success!
192.168.11.46 success!
192.168.11.32 success!
192.168.11.31 success!
192.168.11.34 success!
192.168.11.47 success!
192.168.11.10 success!
192.168.11.45 success!
[root@localhost /data/scripts]#cat ip_while.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: ip_while.sh
#********************************************************************
NETID=192.168.11.
declare -i HOSTID=1
while [ $HOSTID -lt 254 ];do
{
	ping -c1 -W1 $NETID$HOSTID &>/dev/null
	if [ $? -eq 0 ];then
		echo "$NETID$HOSTID success!"
	else
		echo "$NETID$HOSTID fail!"
	fi
}&
	let HOSTID++
done
wait

3.每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间.

[root@localhost /data/scripts]#cat etc_backup.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: etc_backup.sh
#********************************************************************
[ -d /backup ] || mkdir /backup
rpm -q xz >/dev/null
[ $? -eq 0 ] || yum -y install xz >/dev/null
DATEFORMAT=`date -d yesterday "+%Y-%m-%d-%H"`
tar -Jcf /backup/etcbak-$DATEFORMAT.tar.xz /etc &> /dev/null
[root@localhost /data/scripts]#vim /etc/crontab 

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=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
30 1 * * 0 root sh /data/scripts/etc_backup.sh &>/dev/null

4.工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就发送邮件报警.

[root@localhost /data/scripts]#bash check_disk.sh 
100
Warning!!! 
 /dev/sr0‘s avaliable space is less than 20%|mail -s Warning root
[root@localhost /data/scripts]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       100G  1.7G   99G   2% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M   72M  919M   8% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda3        50G  169M   50G   1% /data
/dev/sda1      1014M  127M  888M  13% /boot
/dev/sr0        4.3G  4.3G     0 100% /mnt/cdrom
tmpfs           199M     0  199M   0% /run/user/0
[root@localhost /data/scripts]#cat check_disk.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: check_disk.sh
#********************************************************************
Usage_Rate=`df |awk -F ‘[ %]+‘ ‘BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}‘`
Partition_Name=`df |grep "$Usage_Rate%" |cut -d" " -f1`
echo $Usage_Rate
if [ $Usage_Rate -gt 80 ];then
	echo -e "Warning!!! \n $Partition_Name‘s avaliable space is less than 20%|mail -s Warning root"
fi

*/10 * * * 1-5 root sh /data/scripts/check_disk.sh &>/dev/null

八、shell脚本进阶、系统进程与计划。

上一篇:编写shell脚本自动生成开头注释简介


下一篇:关闭SELinux的两种方法