一、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
1.安装邮件服务
[root@server ~]# yum install s-nail -y
[root@server ~]# vim /etc/s-nail.rc #写入以下内容
2.编写脚本
[root@server ~]# vim disk1.sh
3.编写系统计划任务
[root@server ~]# vim /etc/crontab
执行脚本测试:
二、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
1.查看进程的方式判断该程序是否运行
[root@server ~]# vim /web1 #写入以下内容
#!/bin/bash
ps=$(ps -ef | grep httpd | grep -v grep | wc -l)
if (($ps>0))
then
echo "web服务已启动"
else
echo "web服务未启动"
yum install httpd -y > /dev/null
systemctl start httpd
systemctl start firewalld
# 向域中添加http放行策略
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 "web服务正在运行!"
fi
测试:
2、通过查看端口的方式判断该程序是否运行
[root@server ~]# ss -lntup | grep -w 80 | wc -l
三、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
[root@server ~]# vim curl1.sh
#!/bin/bash
ip=$(ip a | grep ens160 | grep inet | cut -d "/" -f1 | tr -s " " | cut -d " " -f3)
curl -s $ip > /dev/null
if (($?==0))
then
echo "web服务能正常访问"
else
echo "web服务不能正常访问"
exit 12
fi
测试: