shell第一次练习

第一题

安装邮件服务

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值