shell脚本任务

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

[root@Server-NFS-DNS ~]# yum install mailx -y
[root@Server-NFS-DNS ~]# vim /etc/mail.rc 
[root@Server-NFS-DNS ~]# vim disk.sh
[root@Server-NFS-DNS ~]# bash disk.sh

        set from=1658635147@qq.com
        set smtp=smtp.qq.com
        set smtp-auth-user=1658635147@qq.com
        set smtp-auth-password=jeez.....gei
        set smtp-auth=login

编写脚本

#!/bin/bash

disk=$(df -m | grep -w "/" | tr -s " " |cut -d " " -f4)
str1="Warning! disk space less than 20G!"
if [ "$disk" -lt 20000 ]
then    
        echo "$str1" | mail -s "$str1" 1658635147@qq.com
fi  

测试能否发送

[root@Server-NFS-DNS ~]# bash disk.sh

编写周期性计划

[root@Server-NFS-DNS ~]# vim  /etc/crontab
0 0 * * *  root  /bin/bash  /root/disk1.sh


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

[root@Server-NFS-DNS ~]# vim web.sh

#!/bin/bash

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

if [ "$ps" -gt 0 ]
then    
        echo "htppd 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   

[root@Server-NFS-DNS ~]# bash web.sh

测试


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

[root@node ~]# vim web1.sh

#!/bin/bash

curl -s 192.168.118.131 > /dev/null

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

测试

相关推荐

  1. linux centos 定时任务,执行shell脚本

    2024-04-27 03:38:04       14 阅读
  2. 自动化任务:探索 Shell 脚本的实际应用

    2024-04-27 03:38:04       35 阅读
  3. Shell脚本入门实战:探索自动化任务与实用场景

    2024-04-27 03:38:04       31 阅读
  4. Shell脚本

    2024-04-27 03:38:04       30 阅读
  5. shell脚本

    2024-04-27 03:38:04       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-27 03:38:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-27 03:38:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-27 03:38:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-27 03:38:04       18 阅读

热门阅读

  1. 内网pth横向渗透思路笔记

    2024-04-27 03:38:04       13 阅读
  2. vue用法示例(三)

    2024-04-27 03:38:04       11 阅读
  3. [Android]Jetpack Compose自定义主题

    2024-04-27 03:38:04       10 阅读
  4. STM32 JTAG

    2024-04-27 03:38:04       13 阅读
  5. 好用的项目管理系统推荐,项目人必看!

    2024-04-27 03:38:04       12 阅读
  6. 鸿蒙小案例-搜索高亮

    2024-04-27 03:38:04       13 阅读
  7. MongoDB聚合运算符:$replaceOne

    2024-04-27 03:38:04       11 阅读
  8. Mybatis之if标签判断boolean值

    2024-04-27 03:38:04       14 阅读
  9. look-behind requires fixed-width pattern_正则表达式

    2024-04-27 03:38:04       14 阅读
  10. C++ Primer Plus

    2024-04-27 03:38:04       12 阅读