任务要求:
一.判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
二.判断web服务器是否运行(1.查看进程的方式判断该程序是否运行;2通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
三.使用curl命令访问第二题的web服务,看能否正常访问,如果能,则返回web server is running;反之,则返回12状态码。
1.安装或更新“s-nail”邮件服务
[root@server ~]# yum install s-nail -y
配置邮件客户端
[root@server ~]# vim /etc/s-nail.rc
set from=2395727946@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2395727946@qq.com
set smtp-auth-password=123456789
set smtp-quth=login
*在/etc/s-nail.rc最后一行输入以下内容,第四行为授权码,退出该文件时,需使用“:wq!”命令
编写脚本文件
[root@server ~]# vim test1.sh
disk=`df -m | grep -w "/" | cut -d " " -f5'
str="the space of this disk less than 20G"
if [ "$disk" -lt 20000]
then
echo"$str" | mail -s "$str" 2395727946@qq.com
fi
设置周期性定时任务
[root@server ~]# vim /etc/crontab
在/etc/crontab里最后一行输入命令
0 0 * * * root /bin/bash /root/test1.sh
2.编写命令
[root@server ~]# vim test2.sh
#!/bin/bash
ps='ps -ef | grep "httpd" | grep -v "grep" | wc -l'
str="httpd is already running"
if [ "$ps" -gt 0 ]then echo "$str"else
echo "httpd not found,waiting......" yum install httpd -y &> /dev/null systemctl start httpd systemctl start firewalld firewall-cmd --permanent --zone=public --add-service=http > /d
ev/null firewall-cmd --permanent --zone=public --add-port=80/tcp > /dev/null
firewall-cmd --reload > /dev/null
echo "$str"
fi
测试脚本
[root@server ~]# bash test2.sh
3.编写命令
[root@server ~]# vim test3.sh
#!/bin/bash
curl -s 192.168.186.130 > /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web not accessible"
exit 12
fi
测试脚本
[root@server ~]# bash test3.sh
web server is running
本文描述了如何通过Shell脚本监控磁盘空间,当空间不足时发送邮件通知管理员;检查并启动Web服务器,配置防火墙规则,以及使用curl验证Web服务的可用性。

被折叠的 条评论
为什么被折叠?



