shell脚本

文章讲述了如何使用shell脚本来监测服务器的磁盘空间,当剩余空间小于20GB时发送报警邮件,并通过crontab设定每日检查。同时,脚本检查web服务是否运行,若未运行则自动安装httpd,启动服务并配置防火墙规则。此外,还展示了通过curl命令检测Web服务是否可正常访问。
摘要由CSDN通过智能技术生成

判断当前磁盘剩余空间是否有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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值