shell基础实验(1)

1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查次磁盘剩余空间。

1.1.安装邮件服务,配置邮件服务

[root@server ~]# yum install mailx -y
[root@server ~]# vim /etc/mail.rc 
set from=1580540058@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1580540058@qq.com
set smtp-auth-password=ulfnzcwkcgpjichg
set smtp-auth=login

1.2编写脚本

磁盘在df -m里把可用空间提取出来

[root@server ~]# vim disk1.sh
#!/bin/bash
disk=`df -m | grep -w "/" | tr -s " " | cut -d " " -f4`
str1="warnig:disk space less than 20G!"
if [ "$disk" -lt 20000 ]
then
        echo  "$str1"  |  mail -s  "$str1"  1580540058@qq.com
fi

1.3系统计任务

[root@server ~]# vim /etc/crontab
0 0 * * * root /bin/bash  /root/disk1.sh

先用每时每分测试写* * * * *

1.4测试:

[root@server ~]# bash disk1.sh


2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过査看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。

思路:在编写脚本时可以使用端口判断和进程判断两种方法,这里我们可以把进程抓出来,然后判断是否大于0,大于已经运行,小于则没有运行

通过端口判断:ss -lntup | grep -w 80 | wc -l

通过进程判断:ps -ef | grep nginx | grep -v grep | wc -l

2.1编写脚本

[root@server ~]# vim web1.sh
#!/bin/bash

ps=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ "$ps" -gt 0 ];then
        echo "nginx is already running"
else
        echo "nginx is not started,waiting......"
        yum install nginx -y > /dev/null #提醒信息扔到黑洞里
        systemctl start nginx
        systemctl start firewalld
        firewall-cmd --permanent --zone=public --add-service=http > /dev/null
        firewall-cmd --permanent --zone=public --add-port=80/tcp > /dev/null
#开放http和80端口
        firewall-cmd --reload > /dev/null  #重置防火墙策略
        echo "nginx is already running!"
fi

2.2测试

[root@server ~]# bash web1.sh

[root@server ~]# ss -lntup | grep -w 80 | wc -l

两个80端口条目数


3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web serveris running;如果不能正常访问,返回12状态码。

3.1编写脚本

vim web2.sh
#!/bin/bash
ip=`ip a | grep ens32 | 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 server not accessible!"
        exit 12
fi

3.2测试:

[root@server ~]# bash web2.sh

把web关了,试一下12出来不,出来则成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值