Shell脚本之磁盘空间判断,服务状态判断

文章讲述了如何通过编写bash脚本监控服务器磁盘空间,当磁盘空间低于20G时发送报警邮件,同时检查并启动Web服务并配置防火墙。还演示了使用curl验证Web服务是否运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.第一题

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

第一步:安装邮件服务并配置

[root@server ~]# yum install s-nail  -y
[root@server ~]# vim /etc/s-nail.rc
set  from=2034795900@qq.com
set  smtp=smtp.qq.com
set  smtp-auth-user=2034795900@qq.com
set  smtp-auth-password=bqcisaibnhxtbaef
set  smtp-auth=login

image-20230831192633313

第二步:编写脚本

[root@server ~]# vim disk1.sh
#!/bin/bash
​
a=$(($( df | grep -w / | tr -s " " | cut -d " " -f4)/1024/1024))
​
if (($a >=20 ))
then
        echo "磁盘剩余空间为$a G"
else
        echo "磁盘剩余空间为$a G,小于20G"
        echo "磁盘剩余空间为$a G,小于20G" |mail -s "磁盘剩余空间为$a G,小于20G" 2034795900@qq.com
​
fi

image-20230831192801996

第三步:系统计划任务

[root@server ~]# chmod +x disk1.sh  #设置执行权限
[root@server ~]# vim /etc/crontab
30  19  *  *  * root  /bin/bash /root/disk1.sh

image-20230831192936071

第四步:查看邮箱是否收到消息

image-20230831193259277

2.第二题

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

法一:通过查看进程的方式判断该程序是否运行

第一步:编写脚本

[root@server ~]# vim web.sh
#!/bin/bash
​
ps=$(ps -ef | grep httpd |grep -v grep |wc -l)
​
if(($ps > 0))
then
        echo "httpd is running"
else
        echo "httpd not started"
        yum install httpd -y > /dev/null
        systemctl start httpd
        systemctl start firewalld
        firewall-cmd --permanent --zone=public --add-service=http > /dev/null
        firewall-cmd --reload &> /dev/null
        echo "http is already running"  
fi

image-20230831194551148

第二步:运行测试

[root@server ~]# vim web.sh
#!/bin/bash
​
ps=$(ps -ef | grep httpd |grep -v grep |wc -l)
​
if(($ps > 0))
then
        echo "httpd is running"
else
        echo "httpd not started"
        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-service=80/tcp > /dev/null
        firewall-cmd --reload &> /dev/null
        echo "http is already running"  
fi

image-20230831194805122

法二:通过查看端口的方式判断该程序是否运行

第一步:恢复快照

第二步:编写脚本

[root@server ~]# vim web.sh
#!/bin/bash
​
ps=$(netstat -lntup | grep -w 80 | wc -l )
​
if(($ps > 0))
then
        echo "httpd is running"
else
        echo "httpd not started"
        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 "http is already running"  
fi

image-20230831195641176

第三步:运行测试

[root@server ~]# bash web.sh
httpd not started
http is already running
[root@server ~]# bash web.sh
httpd is running

image-20230831195751685

3.第三题

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

第一步:编写脚本

[root@server ~]# vim judge.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 server is running."
else    
        echo "web not accessible."
        exit 12
fi  

image-20230831200716710

第二步:运行测试

[root@server ~]# bash judge.sh
web server is running.

image-20230831200850035

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值