作业
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
#问题1:
[root@B zuoye6]# rpm -ivh mailx-12.5-43.fc38.x86_64.rpm
[root@B zuoye6]# vim /etc/mail.rc #基于QQ邮箱
set from=XXX@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=XXX@qq.com
set smtp-auth-password=jbswggjuefmydfga
set ssl-verify=ignore
set nss-config-dir=/root/.certs
set smtp-auth=login
[root@B zuoye6]# vim cipan.sh
#!/bin/bash
boot=`df -m /boot | grep / | tr -s " " " " | cut -d " " -f 4 `
g=` df -m / | grep -w / | tr -s " " " " | cut -d " " -f 4`
sum=$((boot+g))
c=$((120*1024 ))
if [ $sum -lt $c ];then
echo 磁盘小于20G | mail -s "报警" XXX@qq.com
fi
[root@B zuoye6]# crontab -e
28 22 * * * bash /root/zuoye6/cipan.sh
#问题2:
[root@B zuoye6]# vim fuwu.sh
#!/bin/bash
s=`ps -aux | grep httpd | wc -l `
if [ $s -gt 2 ];then
echo 进程判断服务已经启动
else
systemctl restart httpd
echo 服务已经启动
fi
netstat -tunlp | grep httpd &> /dev/null
if [ $? -eq 0 ];then
echo 端口判断服务启动
else
systenctl restart httpd
fi
#问题3:
[root@B zuoye6]# bash fuwu_chenggong.sh
#!/bin/bash
curl localhost &> /dev/null
if [ $? -eq 0 ];then
echo web server is running
else
exit 12
fi