1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
#编写脚本文件:
#tip:disk_sy 为磁盘总剩余空间,其他disk_*为各磁盘分区剩余空间
#!/bin/bash
disk_devtmpfs=$(df | grep v$ | awk '{print $4}')
disk_tmpfs1=$(df | grep hm$ | awk '{print $4}')
disk_tmpfs2=$(df | grep un$ | awk '{print $4}')
disk_tmpfs3=$(df | grep p$ | awk '{print $4}')
disk_rhel_root=$(df | grep /$ | awk '{print $4}')
disk_nvme0n1p1=$(df | grep t$ | awk '{print $4}')
disk_tmpfs4=$(df | grep 0$ | awk '{print $4}')
disk_sy=$(($disk_devtmpfs + $disk_tmpfs1 + $disk_tmpfs2 + $disk_tmpfs3 + $disk_rhel_root + $disk_nvme0n1p1 + $disk_tmpfs4))
echo "disk_sy: $disk_sy"
if [ "$disk_sy" -lt 20480 ];then
echo "当前磁盘剩余空间不足20G ,目前剩余$disk_sy" | mail -s "disk_warning" root
else
echo "当前磁盘剩余空间充足,目前剩余${disk_sy}M" | mail -s "disk_report" root
fi
执行效果:
mail命令查看如图:
每天检查磁盘空间:
配置/etc/crontab文件:
#配置命令:
* * * * * root /shell/day28/testT1.sh
隔一分钟查看如图:
2、判断web服务是否运行:如果没有运行,则启动该服务并配置防火墙规则。
①查看进程的方式判断该程序是否运行;
[root@192 chap08]# vim test2T5.sh
#编辑脚本文件内容:
#!/bin/bash
if [ "`ps -ef | grep httpd | wc -l`" -gt 1 ]; then
echo "httpd is running !"
else
echo "httpd is not running!"
systemctl start httpd &> /dev/null
fi
#由于先用法②进行测试,则先停止并查看httpd进程,再执行脚本文件进行法①的测试;
[root@192 chap08]# systemctl stop httpd
[root@192 chap08]# systemctl status httpd
[root@192 chap08]# bash test2T5.sh
效果如图:
②通过查看端口的方式判断该程序是否运行;
[root@192 chap08]# vim test2T5.sh
#脚本文件test2T5.sh内容:
read -p "请输入您想查询的进程:" process
ss -lntup | grep $process &> /dev/null
if [ "$?" -eq 0 ];then
echo "$process is running!"
else
echo "$process is not runnig!"
systemctl start $process
fi
[root@192 chap08]# bash test2T5.sh
#执行脚本文件test2T5.sh
效果如图:
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码
[root@192 day28]# vim testT3.sh
#编写脚本文件内容:
#!/bin/bash
systemctl restart httpd
systemctl stop firewalld
setenforce 0
curl https://192.168.10.137 -k &> /dev/null
if [ "$?" -eq 0 ];then
echo "webserver is running!"
cat /www/https/index1.html
else
echo "12"
fi
[root@192 day28]# bash testT3.sh
#将脚本中ip192.168.10.133改为192.168.10.137进行测试,效果如下图: