1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一
次磁盘剩余空间。
下载邮件服务器
[root@sever ~]# yum install s-nail -y
配置邮件服务
[root@sever ~]# vim /etc/s-nail.rc
set from=2564094127@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2564094127@qq.com
set smtp-auth-password=123456
set smtp-auth=login
编写脚本
[root@sever ~]# vim disk1.sh
#!/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" 2564094127@qq.com
fi
执行时间
[root@sever ~]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
0 0 * * * root/bin/bash /root/disk1.sh
2、判断web服务是否运行 (1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
编写脚本
[root@sever ~]# vim web.sh
#!/bin/bash
ps=`ps -ef | grep httpd | grep -v grep | wc -l `
if [ "$ps" -gt 0 ];
then
echo "Httpd is already running "
else
echo "Httpd not strated "
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@sever ~]# ss -lntup | grep -w 80 |wc -l
0
3、使用curl命令访问第二题的web服务,看能否正常访,如果能正常访问,则返回web serveris running;如果不能正常访问,返回12状态码。
#!/bin/bash
ip =` ip a | grep ens160 | grep inet | cut -d / -f1 | tr -s ' ' | cut -d ' ' -f3 `
curl -s $ip > /dev/null
if (($?==0))
then
echo "web server is running "
else
echo "web not accessible"
exit 12
fi