1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
1.1安装邮件服务并添加邮箱配置
yum install mailx -y
vim /etc/mail.rc
set from=2961953033@qq.com
set smtp-auth-user=2961953033@qq.com
set smtp-auth-password=ilzeurtdljyodheb -------这个码要去QQ邮箱里面去获取
set smtp-auth=login
1.2开始编写脚本
vim test11.sh
#!/bin/bash
test=$(df -m | grep -w "/" | tr -s " " | cut -d " " -f4)
str1="warning:test space less than 20G"
if [ "$test" -lt 20000 ]
then
echo "$str1" | mail -s "$str1" 2961953033@qq.com
fi
1.3设置每天检查一次剩余磁盘空间
vim /etc/crontab
0 0 * * * root /bin/bash /root/test11.sh
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
#!/bin/bash
ps=$(ps -ef | grep nginx | grep -v grep | wc -l)
if [ "$ps" -gt 0 ]
then
echo "nginx is running"
else
echo "nginx is not running,waiting....."
yum install nginx -y > /dev/null
systemctl start nginx
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http > /dev/null
firewall-cmd --permanent --zone=public --add-port=80/tcp > /dev/null
firewall-cmd --reload > /dev/null
echo "nginx is running"
fi
使用端口判断:
#!/bin/bash
ps=$(ps -ef | grep nginx | grep -w 80 | wc -l)
if [ "$ps" -gt 0 ]
then
echo "nginx is running"
else
echo "nginx is not running,waiting....."
yum install nginx -y > /dev/null
systemctl start nginx
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http > /dev/null
firewall-cmd --permanent --zone=public --add-port=80/tcp > /dev/null
firewall-cmd --reload > /dev/null
echo "nginx is running"
fi
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running,如果不能正常访问,则返回12状态码。
#!/bin/bash
ip=$(ip a | grep ens32 | grep inet | cut -d / -f1 | tr -s ' ' | cut -d ' ' -f3)
curl -s $ip > /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web not accessinle"
exit 12
fi