判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
1.编写shell
[root@server ~]# vim disk.sh
#!/bin/bash
disk=$( df -m | grep -w "/" | tr -s " " | cut -d " " -f4 )
if [ $disk -lt 20000 ]
then
echo "warm:the disk space is less than 20G"
fi
2.设置crontab循环周期任务
[root@server ~]# vim /etc/crontab
0 0 * * * root /bin/bash /root/disk.sh
判断web服务是否运行 (1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
1.编写shell脚本 使用查看进程的方式
x=$(ps -ef | grep httpd | grep -v "grep" | wc -l)
if [ $x -ge 1 ]
then
echo "httpd is running"
else
echo "httpd is not running, let's restart"
yum install httpd -y &> /dev/null
systemctl start httpd
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 "httpd is running "
fi
使用查看端口的方式
x=ss -lntup | grep -w 80 | wc -l
2.测试
[root@server ~]# bash test1.sh
httpd is not running, let's restart
Warning: ALREADY_ENABLED: 80:tcp
httpd is running
使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server isrunning;如果不能正常访问,返回12状态码
1.编写脚本
#!/bin/bash
curl -s $(ip a | grep ens160 | grep inet | cut -d "/" -f1 | tr -s " " | cut -d " " -f3) > /dev/null
if (($?==0 ))
then
echo "web is running"
else
echo "web is not running"
exit 12
fi
2.测试
[root@server ~]# bash test2.sh
web is running
[root@server ~]# systemctl stop httpd
[root@server ~]# bash test2.sh
web is not running