1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
编写脚本文件
#!/bin/bash
#########################
#File name:1.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 09:23:06
#Description:
#########################
disk=`df -h | grep -w / | awk '{print $4}' | cut -d G -f 1`
if [ "$disk" -lt 20 ];then
echo "warning:disk space less than 20G!" | mail -s "Warning" root
fi
给文件添加r,x权限
[root@zyf shell4]# chmod a+rx 1.sh
加入计划任务,每天检查一次磁盘剩余空间
[root@zyf shell4]# vim /etc/crontab
0 0 * * * root /shell4/1.sh
2、判断web服务是否运行,如果没有运行,则启动该服务并配置防火墙规则。
(1)查看进程的方式判断该程序是否运行
编写脚本文件
#!/bin/bash
#########################
#File name:2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 10:56:07
#Description:
#########################
ps=`ps -ef | grep httpd | wc -l`
if [ "$ps" -gt 0 ];then
echo "runnung!"
else
systemctl start httpd
systemctl stop firewalld
fi
给文件添加r,x权限,并查看运行结果
[root@zyf shell4]# chmod a+rx 2.sh
(2)通过查看端口的方式判断该程序是否运行
编写脚本文件
#!/bin/bash
#########################
#File name:2_2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 11:20:06
#Description:
#########################
port=`ss -lntup | grep -w 80 | wc -l`
if [ "$port" -gt 0 ];then
echo "runnung!"
else
systemctl start httpd
systemctl stop firewalld
fi
给文件添加r,x权限,并查看运行结果
[root@zyf shell4]# chmod a+rx 2_2.sh
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
编写脚本文件
#!/bin/bash
#########################
#File name:3.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 11:47:29
#Description:
#########################
curl -s 192.168.65.128 > /dev/null
if [[ $? = 0 ]];then
echo "web server is running."
else
exit 12
fi
给文件添加r,x权限,并查看运行结果
[root@zyf shell4]# chmod a+rx 3.sh