1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
因为如果磁盘剩余空间小于20G需要报警发送邮件给管理员,所以需要对管理员的邮箱进行设置
(1)首先安装软件:
[root@server scripts]# yum install -y s-nail
(2)进入邮件配置文件中设置管理员邮件:vim /etc/s-nail.rc
set [email protected]
set smtp=smtp.qq.com
set [email protected]
set smtp-auth-password=dacqjvfivytdiagj
set smtp-auth=login
(3)尝试向管理员发送邮件: echo "test 1" |s-nail -s "title" [email protected]
编写shell脚本free.sh:将以下内容写入
if [ "`df -h | grep /$ | tr -s " " | cut -d" " -f4 | cut -d"G" -f1`" -lt 20 ]
then
echo "管理员快看看你的内存不够20G了" | s-nail -s "title" [email protected]
fi
(4)测试运行:./free.sh
管理员成功的收到了内存不够的警告 !
(5)设置任务计划每天检查一次:crontab -e
0 0 * * * sh /server/scripts/free.sh
[root@server scripts]# crontab -l
0 0 * * * sh /server/scripts/free.sh
2、判断web服务是否运行
(1)查看进程的方式判断该程序是否运行
编写脚本:vim check_httpd.sh
if [ "`ps -aux |grep httpd |grep -v grep | tr -s " " |cut -d " " -f8 |uniq | wc -l`" -lt 2 ]
then
systemctl start httpd
else
echo "httpd服务已经运行了"
fi
测试:./check_httpd.sh
[root@server scripts]# ./check_web.sh
[root@server scripts]# ./check_web.sh
httpd服务已经运行了
(2)通过查看端口的方式判断该程序是否运行,如果没有运行,则启动该服务并配置防火墙规则。
编写脚本:vim check_httpd2.sh
if [ "`netstat -lnupt | grep 80 |wc -l`" -eq 0 ]
then
systemctl start httpd;firewall-cmd --permanent --zone=public --add-port=80/tcp
else
echo "httpd服务已经启动"
fi
测试:./check_httpd2.sh
[root@server scripts]# ./check_web2.sh
success
[root@server scripts]# ./check_web2.sh
httpd服务已经启动
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
编写脚本:vim curl.sh
read -p "请输入一个网址:" w
curl $w
if [ "`echo $?`" -eq 0 ]
then
echo "web server is running"
else
echo "状态码12"
fi
测试:./curl.sh
[root@server scripts]# ./curl.sh
请输入一个网址:www.baidu.com
web server is running
[root@server scripts]# ./curl.sh
请输入一个网址:www.qweqweweqweweeressd.com
curl: (6) Could not resolve host: www.qweqweweqweweeressd.com
状态码12