shell脚本相关作业

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

1. 安装邮件服
[root@server ~]# yum install s-nail -y
[root@server ~]# vim /etc/s-nail.rc
set from=测试邮箱
set smtp=smtp.qq.com
set smtp-auth-user=测试邮箱
set smtp-auth-password=测试邮箱的POP3/IMAP/SMTP/Exchange/CardDAV服务授权码
set smtp-auth=login
# 退出时需要wq! 强制保存退出
# 退出时需要wq! 强制保存退出
2. 编写脚本
[root@server ~]# vim disk1.sh
#!/bin/bash
##############################################################
# File Name: disk1.sh
# Version: V1.0
# Author: Andy_Sun
# Email: Andy_Sun@163.com
# Organization: http://www.cnblogs.com/Andy_Sun/
# Created Time : 2023-09-01 21:58:09
# Description:
##############################################################

disk=$(df -m | grep -w "/" | tr -s " " | cut -d " " -f4)

str1="warning disk space less than 20G!"

if [ "$disk" -lt 200000 ]
then
        echo "$str1" | mail -s "$str1" 测试邮箱
fi
3. 系统计划任务
[root@server ~]# vim /etc/crontab
* * * * * root /bin/bash /root/disk1.sh  #方便测试这里改为一分钟发一次
4.测试结果

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

[root@server ~]# vim web.sh
#!/bin/bash
##############################################################
# File Name: web.sh
# Version: V1.0
# Author: Andy_Sun
# Email: Andy_Sun@163.com
# Organization: http://www.cnblogs.com/Andy_Sun/
# Created Time : 2023-09-01 22:26:45
# Description:
##############################################################

ps=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ "$ps" -gt 0 ]
then
        echo "Httpd is already 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=http > /dev/null
        firewall-cmd --permanent  --zone=public --add-port=80/tcp > /dev/null
        firewall-cmd --reload > /dev/null
        echo "Httpd is already running"
fi

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

[root@server ~]# vim web_curl.sh
#!/bin/bash
##############################################################
# File Name: web_curl.sh
# Version: V1.0
# Author: Andy_Sun
# Email: Andy_Sun@163.com
# Organization: http://www.cnblogs.com/Andy_Sun/
# Created Time : 2023-09-01 22:35:45
# Description:
##############################################################

curl -s 192.168.240.130 > /dev/null   #192.168.240.130为本机ip
if (($?==0))
then
        echo "web server is running!"
else
        echo "Web not accessible!"
        exit 12
fi

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值