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)

相关推荐
甲鱼92911 小时前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
YuMiao2 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
碳基沙盒3 天前
OpenClaw 多 Agent 配置实战指南
运维
Sinclair4 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean5 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
蝎子莱莱爱打怪6 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
茶杯梦轩6 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
海天鹰6 天前
【免费】PHP主机=域名+解析+主机
服务器
DianSan_ERP6 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅6 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑