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 测试 ![](https://file.jishuzhan.net/article/1783293665628655618/2c40d59d4e2ce17f537768266ad35eda.webp) 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 测试 ![](https://file.jishuzhan.net/article/1783293665628655618/c1990bced6d57556344b4763b977f659.webp)

相关推荐
xiaobaishuoAI1 分钟前
后端工程化实战指南:从规范到自动化,打造高效协作体系
java·大数据·运维·人工智能·maven·devops·geo
郝学胜-神的一滴4 分钟前
Linux系统编程:深入理解读写锁的原理与应用
linux·服务器·开发语言·c++·程序人生
代码游侠13 分钟前
学习笔记——ESP8266 WiFi模块
服务器·c语言·开发语言·数据结构·算法
Hello.Reader17 分钟前
PyFlink Configuration 一次讲透怎么配、配哪些、怎么“调得快且稳”
运维·服务器·python·flink
__雨夜星辰__18 分钟前
VMware 17 下 Ubuntu 虚拟机与宿主机间复制粘贴失效问题
linux·运维·ubuntu
云和数据.ChenGuang19 分钟前
Uvicorn 是 **Python 生态中用于运行异步 Web 应用的 ASGI 服务器**
服务器·前端·人工智能·python·机器学习
prettyxian21 分钟前
【linux】进程调度:优先级、时间片与O(1)算法
linux·运维·服务器
__雨夜星辰__22 分钟前
VS Code 的Remote-SSH/Remote Development插件无法连接到 Ubuntu 系统下 的远程虚拟主机(VMware)
运维·vscode·ubuntu·ssh
2401_8658548828 分钟前
16核32g的服务器能运用于哪些场景
运维·服务器
jerryinwuhan28 分钟前
期末总复习
linux·运维