1.判断当前磁盘剩余空间是否20g,若小于20g,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间
1.邮件设置
[root@server ~]# yum install s-nail -y
[root@server ~]# vim /etc/s-nail.rc
set from=923452780@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=923452780@qq.com
set smtp-auth-password=iytwnyidnpxjbdbf
set smtp-auth=login
2.shell脚本配置
#!/bin/bash
disk= `df -h | grep -w / | cut -d " " -f9`
str1="空间小于20g"
if [ $disk < 20000 ]
then
echo "$str1" | mail -s "$str1" 923452780@qq.com
fi
3.crontab 配置
[root@server ~]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
# | | | | |
# * * * * * user-name command to be executed
0 0 * * * root /bin/bash /root/disk1.sh
2.判断web服务是否运行(1.查看进程的方式判断该程序是否运行,2.通过查看端口的方式判断程序是否运行)如果没有运行,则启动该服务配置并配置防火墙规则。
[root@server ~]# vim web1.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 is not start , please 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 ~]# bash web1.sh
httpd is not start , please waiting...
httpd is already running
[root@server ~]# ps -ef | grep "httpd"
root 3321 1 0 14:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 20005 3321 0 14:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 20006 3321 0 14:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 20007 3321 0 14:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 20009 3321 0 14:51 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 32099 1253 0 14:52 pts/0 00:00:00 grep --color=auto httpd
3.使用curl命令访问第二题的web服务,看能否正常访问,如果可以,则返回web server is running ;如果不能 ,则返回12状态码
[root@server ~]# vim web2.sh
#!/bin/bash
curl -s 192.168.37.150 > /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web not accessible"
exit 12
fi
wq
[root@server ~]# bash web2.sh
web server is running