SHELL小作业1

该文章描述了如何编写bash脚本来检查Linux服务器的磁盘剩余空间,如果少于20GB则发送报警邮件。同时,通过检查进程和端口来判断HTTP服务是否运行,并在未运行时自动启动及配置防火墙规则。最后,使用curl命令验证Web服务的可达性。
摘要由CSDN通过智能技术生成

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

看一下内存容量

[root@server ~]#free -h

截出来内存剩余空间

 [root@server ~]# free  -m | grep  Mem | tr -s " " | cut -d " " -f4

 编写脚本文件

[root@server ~]# vim  test.sh

#!/bin/bash

free_men=$(free -m | grep Mem | tr -s " " | cut -d " " -f4)

notice= "警告,剩余内存容量为;$free_mem,低于20480m"

if (($free_men<20480))
then
        echo "$notice" | mali -s "$notice" 1049545439@qq.com
fi

 编写时间脚本:

准备工作

[root@server ~]# yum install httpd -y

[root@server ~]# systemctl restart httpd

[root@server ~]# systemctl status httpd

[root@server ~]# vim  /etc/crontab

在末行加上

0 0 * * * root /bin/bash /root/test.sh
 

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

编写脚本

[root@server ~]# vim web1.sh

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

#!/bin/bash

ps=` ps  -ef | grep  httpd | grep -v grep | wc -l`

if [ "$ps" -gt 0 ]
then 
        echo "httpd is running"
else 
        echo "httpd not started ,waiting..."
        yum install httpd -y &> /dev/null
        systemctl start httpd
        systemctl start firewalld
        firewall-cmd --permanent --zone=public --add-service=httpd > /dev/null
        firewall-cmd --permanent --zone=public --add-port=80/tcp   > /dev/null
        firewall-cmd --reload > /dev/null
        echo "httpd is already running"
fi

 

 

2、通过查看端口的方式

#!/bin/bash

ps=`netstat -lnput | grep 80 | wc -l`
if [ "$ps" -gt 0 ]
then 
        echo "httpd is running"
else 
        echo "httpd not started ,waiting..."
        yum install httpd -y &> /dev/null
        systemctl start httpd
        systemctl start firewalld
        firewall-cmd --permanent --zone=public --add-service=httpd > /dev/null
        firewall-cmd --permanent --zone=public --add-port=80/tcp   > /dev/null
        firewall-cmd --reload > /dev/null
        echo "httpd is already running"
fi
fi

 

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

[root@server ~]# vim web2.sh

#!/bin/bash

curl -s 192.168.248.130 > /dev/null

if (($?==0))
then
        echo "web server is running"
else
        echo "web not accessible"
        exit 12
fi
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值