1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
- # 为了方便测式使用每分钟发送一封邮件,若想要每天定时检查则需要将前两个*改为0 0
- [root@timeserver ~]# crontab -e
- * * * * * (($(df -m | grep -w "/" | tr -s " " | cut -d " " -f4)<20408)) && echo "内存空间不足" | mail -s "内存空间不足" 3291614481@qq.com

发送邮件时间

判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
- [root@timeserver ~]# vim test2.sh
- x=`ps -ef | grep "httpd" | grep "apache" | wc -l`
- if (("$x" > 0 ))
- then
- echo "httpd is already running "
- else
- echo " httpd not started ,waiting...."
- yum install httpd -y &> /dev/null
- systemctl restart httpd
- systmctl disable --now firewalld
- setenforce 0
- echo "httpd is already running! "
- fi
Httpd开启时显示:

Httpd关闭时显示:三
三.检查是否成功访问http服务器
使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web serveris running; 如果不能正常访问,返回12状态码。
- [root@timeserver ~]# vim test3.sh
- curl -s 192.168.186.200
- if (($?==0 ))
- then
- echo "web server is running!" > /dev/null
- else
- echo "web not accessible"
- exit 12
- fi
成功访问

访问失败