第一题
安装邮件服务
[root@server ~]# yum install s-nail -y
配置邮件服务
[root@server ~]# vim /etc/s-nail.rc
set from=2518582839@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2518582839@qq.com
set smtp-auth-password=ykewnyxyxgeweabg
set smtp-auth=login
编写判断当前磁盘空间大小,比较小于20G,发送报警信息
[root@server ~]# vim zuoye1.sh
#!/bin/bash
free=$(df -m | grep rhel |tr -s " " | cut -d " " -f4)
if [ $free -lt 20000 ]
then echo "当前剩余内存还有$free" | mail -s $free 2518582839@qq.com
fi
编写定时计划
[root@server ~]# vim /etc/crontab
0 0 * * * root /bin/bash /root/zuoye1.sh
第二题
在一台未安装httpd虚拟机测试
[root@server ~]# vim zuoye2.sh
#!/bin/bash
yx=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ $yx -gt 0 ]
then
echo "httpd is already running"
else
echo "httpd is not running,waitting"
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 zuoye2.sh
httpd is not running,waitting
httpd is already running!!!
在一台安装了httpd,且正在运行的虚拟机测试
[root@node1 ~]# vim zuo.sh
#!/bin/bash #命令解释器
yx=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ $yx -gt 0 ]
then
echo "httpd is already running"
else
echo "httpd is not running,waitting"
yum install httpd -y &> /dev/null
systemctl start httpd
systenctl 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@node1 ~]# bash zuo.sh
httpd is already running
第三题
[root@server ~]# vim zuoye3.sh
#!/bin/bash
curl -s 192.168.40.128 &> /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web is not running"
exit 12
fi
[root@server ~]# bash zuoye3.sh
web server is running