题目:
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
一、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
1.安装邮件服务器: dnf install -y s-nail
2.配置管理员邮件:
[root@server ~]# vim /etc/s-nail.rc
set from=2947149465@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2947149465@qq.com
set smtp-auth-password=??? # 你的邮箱授权码
set smtp-auth=login
3.编写脚本:
4.查看结果:
5.编写计划任务:
[root@server scripts]# crontab -e
0 0 * * * sh /scripts/test1.sh &>/dev/null
二、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
1.安装httpd包,关闭防火墙和selinux
[root@server ~]# dnf install -y httpd
[root@server ~]# setenforce 0
[root@server ~]# systemctl stop firewalld.service
2.查看进程方式:
1)编写脚本:
[root@server scripts]# vim test2.sh +
total=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ $total -gt 0 ]
then
echo "hello"
else
systemctl stop firewalld
systemctl start httpd
fi
2)加可执行权限,测试:
[root@server scripts]# chmod +x test2.sh
[root@server scripts]# systemctl stop httpd
[root@server scripts]# ./test2.sh
[root@server scripts]# ./test2.sh
hello
3.查看端口方式进行判断:
[root@server scripts]# vim test3.sh +
total=$(netstat -lnupt | grep 80 |wc -l)
if [ $total -gt 2 ]
then
echo "hello"
else
systemctl stop firewalld
systemctl start httpd
fi
[root@server scripts]# chmod +x test3.sh
[root@server scripts]# systemctl stop httpd
[root@server scripts]# ./test3.sh
[root@server scripts]# ./test3.sh
hello
三、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
[root@server scripts]# vim test4.sh +
if curl 192.168.124.132 &>/dev/null
then
echo "web server is running"
else
exit 12
fi
[root@server scripts]# chmod +x test4.sh
[root@server scripts]# systemctl stop httpd
[root@server scripts]# echo $?
12
[root@server scripts]# ./test4.sh
web server is running