shell流程控制之条件判断练习

文章介绍了如何通过Shell脚本来实现磁盘空间监测,当剩余空间低于20GB时发送报警邮件给管理员,并设置了每日检查任务。同时,提供了两种方法检查和确保Web服务的运行状态,包括通过进程检查和端口监听。最后,展示了使用curl命令验证Web服务的可达性,并根据响应状态码给出反馈。
摘要由CSDN通过智能技术生成

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

因为如果磁盘剩余空间小于20G需要报警发送邮件给管理员,所以需要对管理员的邮箱进行设置

(1)首先安装软件:

[root@server scripts]# yum install -y s-nail

(2)进入邮件配置文件中设置管理员邮件:vim /etc/s-nail.rc

set from=1281984647@qq.com             
set smtp=smtp.qq.com                   
set smtp-quth-user=1281984647@qq.com   
set smtp-auth-password=dacqjvfivytdiagj
set smtp-auth=login  

(3)尝试向管理员发送邮件: echo "test 1" |s-nail -s "title" 1281984647@qq.com

编写shell脚本free.sh:将以下内容写入

if  [  "`df -h | grep /$ | tr -s " " | cut -d" " -f4 | cut -d"G" -f1`"  -lt 20 ]   
then                                                                               
         echo "管理员快看看你的内存不够20G了" | s-nail -s "title" 1281984647@qq.com
fi   

(4)测试运行:./free.sh

管理员成功的收到了内存不够的警告 !

(5)设置任务计划每天检查一次:crontab -e

0 0 * * * sh /server/scripts/free.sh
[root@server scripts]# crontab -l
0 0 * * * sh /server/scripts/free.sh

2、判断web服务是否运行

(1)查看进程的方式判断该程序是否运行

编写脚本:vim check_httpd.sh

if [  "`ps -aux |grep httpd |grep -v grep | tr -s " " |cut -d " " -f8 |uniq | wc -l`" -lt 2 ]
then                           
    systemctl start httpd      
else                           
    echo "httpd服务已经运行了"                                                    
fi 

测试:./check_httpd.sh

[root@server scripts]# ./check_web.sh 
[root@server scripts]# ./check_web.sh 
httpd服务已经运行了

(2)通过查看端口的方式判断该程序是否运行,如果没有运行,则启动该服务并配置防火墙规则。

编写脚本:vim check_httpd2.sh

if [ "`netstat -lnupt | grep 80 |wc -l`" -eq 0  ]                                  
then                                                                               
    systemctl start httpd;firewall-cmd --permanent --zone=public --add-port=80/tcp
else                                                                               
    echo "httpd服务已经启动"                                                       
fi

测试:./check_httpd2.sh

[root@server scripts]# ./check_web2.sh 
success
[root@server scripts]# ./check_web2.sh 
httpd服务已经启动

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

编写脚本:vim curl.sh

read -p "请输入一个网址:" w
curl $w         
if  [ "`echo $?`" -eq 0  ]
then            
    echo "web server is running"
else            
    echo "状态码12"
fi   

测试:./curl.sh

[root@server scripts]# ./curl.sh 
请输入一个网址:www.baidu.com
web server is running

[root@server scripts]# ./curl.sh 
请输入一个网址:www.qweqweweqweweeressd.com
curl: (6) Could not resolve host: www.qweqweweqweweeressd.com
状态码12
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未知百分百

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值