1. 磁盘空间检查并发送邮件报警
首先,确保您的系统已安装mailx
和配置好了邮件发送的相关设置。接着,您可以使用如下步骤创建和配置脚本以及定时任务:
-
安装
mailx
:
bash yum install mailx -y
-
配置邮件发送设置 (
/etc/mail.rc
):
bash vim /etc/mail.rc
在文件中添加以下内容:
plaintext set from=your-email@qq.com set smtp=smtp.qq.com set smtp-auth-user=your-email@qq.com set smtp-auth-password=your-password set smtp-auth=login
-
创建磁盘空间检查脚本 (
disk1.sh
):
bash vim disk1.sh
添加以下脚本内容:
```bash
#!/bin/bash
disk=$(df -m | grep -w “/” | tr -s " " | cut -d " " -f4)
str1=“Warning: Disk space less than 20G!”
if [ “
d
i
s
k
"
−
l
t
20000
]
;
t
h
e
n
e
c
h
o
"
disk" -lt 20000 ]; then echo "
disk"−lt20000];then echo"str1” | mail -s “$str1” your-email@qq.com
fi
```
保存并退出编辑器。
-
设置脚本可执行权限:
bash chmod +x disk1.sh
-
设置定时任务:
bash crontab -e
添加以下行来执行脚本每天午夜检查一次:
plaintext 0 0 * * * /root/disk1.sh
2. 检查Web服务状态并启动服务
-
创建检查和启动服务的脚本 (
web1.sh
):
bash vim web1.sh
添加以下内容:
bash #!/bin/bash if ps -ef | grep -q "[h]ttpd"; then echo "HTTPD is already running" elif ss -lnt | grep -qw ":80"; then echo "Port 80 is open, HTTPD might be running" else echo "HTTPD is not running, starting service..." yum install httpd -y &> /dev/null systemctl start httpd systemctl start firewalld firewall-cmd --permanent --zone=public --add-service=http firewall-cmd --permanent --zone=public --add-port=80/tcp firewall-cmd --reload echo "HTTPD is now running!" fi
保存并退出编辑器。 -
设置脚本可执行权限:
bash chmod +x web1.sh
3. 使用curl命令检查Web服务可访问性
-
创建Web服务可访问性检查脚本 (
web2.sh
):
bash vim web2.sh
添加以下内容:
bash #!/bin/bash if curl -s 192.168.48.130 > /dev/null; then echo "web server is running" else echo "web not accessible" exit 12 fi
保存并退出编辑器。 -
设置脚本可执行权限:
bash chmod +x web2.sh