1:
第一步:安装邮件服务
[root@server ~]# yum install s-nail -y
第二步:编辑配置文件
[root@server ~]# vim /etc/s-nail.rc
set from=自己的qq邮箱地址
set smtp=smtp.qq.com
set smtp-auth-user=自己的qq邮箱地址
set smtp-auth-password=自己的邮箱授权码
set smtp-auth=login
第三步:编写脚本
[root@server ~]# vim disk.sh
#!/bin/bash
disk1=$(df -m | grep -w / |tr -s " "|cut -d " " -f4)
str1="warning disk space less than 20G"
if [ $disk1 -lt 20000 ]
then
echo "$str1" | mail -s "$str1" 自己邮箱地址
fi
第四步:编写周期性任务
[root@server ~]# vim /etc/crontab
为了看见效果,采用1一分钟一发
* * * * * root /bin/bash /root/disk.sh
2:
编写脚本web.sh
#!/bin/bash
ps=$(ps -ef |grep httpd |grep -v "grep" | wc -l)
if [ $ps -gt 0 ]
then
echo "httpd 已经启动"
else
echo "httpd未启动,请稍等正在启动."
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服务已经启动"
fi
[root@server ~]# bash web.sh
httpd未启动,请稍等正在启动.
httpd服务已经启动
3:
#!/bin/bash
curl -s 192.168.253.128 > /dev/null
if (($?==0))
then
echo "web服务已经启动"
else
echo "web服务没有启动"
exit 12
fi
[root@server ~]# bash web2.sh
web服务已经启动