1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
QQ邮箱服务:
[root@server ~]# yum install mailx -y
[root@server ~]# vim /etc/mail.rc
set from=3132541484@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=3132541484@qq.com
set smtp-auth-password=do swc...fih
set smtp-auth=login
创建脚本:
[root@server ~]# vim disk.sh
编写脚本:
disk=$(df -m | grep -w "/" | tr -s " " | cut -d " " -f4)
if [ "$disk" -lt 20000 ]
then
echo "waring:disk sapce less than 20G" | mail -s "waring:disk sapce less than 20G" 3132541484@qq.com
fi
编写系统任务计划:
[root@server ~]# vim /etc/crontab
0 0 * * * root /bin/bash /root/disk.sh
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过査看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
创建脚本:
[root@server ~]# vim web.sh
编写脚本:
ps=$(ps -ef | grep nginx | grep -v grep | wc -l)
if [ "$ps" -gt 0 ]
then
echo "nginx is running"
else
echo "nginx is not started,waiting..."
yum install nginx -y > /dev/null
systemctl start nginx
systemctl start firewalld
firewalld-cmd --permanent --zone=public --add-service=http >/dev/null
firewalld-cmd --permanent --zone=public --add port=80/tcp
firewalld-cmd --reload > /dev/null
echo "nginx is aready running"
fi
测试:
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web serveris running;如果不能正常访问,返回12状态码。
创建脚本:
[root@server ~]# vim web_curl.sh
编写脚本:
ip=$(ip a | grep ens32 | grep inet | cut -d / -f1 | tr -s " " | cut -d " " -f3)
curl -s $ip > /dev/null
if (($?==0))
then
echo "web server is runing"
else
echo "web not accesible"
exit 12
fi
测试: