shell流程条件控制判断 rhce(26)

目录

1.判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件送给管理员,每天检查一次磁盘剩余空间

2.判断web服务是否运行

(1)查看进程的方式判断该程序是否运行;如果没有运行,则启动该服务

(2)通过查看端口的方式判断该程序是否运行,如果没有运行,则启动该服务

3、使用curl命令访问第二题的web服务,看是否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,则返回12状态码


1.判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件送给管理员,每天检查一次磁盘剩余空间

第一步:

编辑shell命令

[root@localhost ~]# vim test1.sh
[root@localhost ~]# cat test1.sh

#!/bin/bash
n1=`df -m | grep -w /  | tr -s " " " " | cut -d " " -f 4`
n2=`df -m | grep /boot  | tr -s " " " " | cut -d " " -f 4`
sum = $((n1+n2))
sum1 = $((sum/1024))
echo $sum1
if [$sum1 -lt 20 ] ;then
    echo 警报
fi

第二步:

执行周期性任务

[root@localhost ~]# crontab -e
[root@localhost ~]# crontab -l

00 09 * * * bash /root/test1.sh

2.判断web服务是否运行

(1)查看进程的方式判断该程序是否运行;如果没有运行,则启动该服务

(2)通过查看端口的方式判断该程序是否运行,如果没有运行,则启动该服务

[root@localhost ~]# vim test2.sh
[root@localhost ~]# cat test2.sh

#!/bin/bash
(1)通过查看进程的方式判断:
count = `ps -aux | grep httpd | wc -l`
if [ $? -gt 2 ] ;then
        echo 服务已运行
else
        systemctl restart httpd
fi
(2)通过查看端口的方式判断:
netstat -tunlp | grep httpd
if [ $? -eq 0 ] ;then
        echo 服务已运行
else
        systemctl restart httpd
fi

3、使用curl命令访问第二题的web服务,看是否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,则返回12状态码

[root@localhost ~]# vim test3.sh
[root@localhost ~]# cat test3.sh

#!/bin/bash
curl http://localhost &> /dev/null
if [ $? -eq 0 ] ;then
        echo web server is running
else
        exit 12
fi

你可能感兴趣的:(linux,运维,服务器)