一、 判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
- 首先进入vim编辑文档,创建work1.sh
- 编写脚本
#!/bin/bash
work1_ci=$(df | grep /$ | tr -s " " | cut -d" " -f4)
if [ "$work1_ci" -le 20971520 ];then
echo "剩余磁盘空间:${work1_ci},低于20G" | mail -s warning root@localhost
fi
- 加可执行权限
chmod +x work1.sh
./work1.sh
二、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
#!/bin/bash
ps_https=`ps -ef | grep httpd | grep -v grep | wc -l`
if [[ $ps_httpd > 0 ]];then
echo "httpd.service is running."
else
systemctl start httpd
systemctl stop firewalld
fi
三、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
#!/bin/bash
curl -s 192.168.5.130 > /dev/null
if [[ $? = 0 ]];then
echo"web server is running."
else
exit 12
fi