shell脚本练习

题目:

1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
​2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
​3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

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

1.安装邮件服务器: dnf  install -y s-nail

2.配置管理员邮件:

[root@server ~]# vim /etc/s-nail.rc 

set from=2947149465@qq.com             
set smtp=smtp.qq.com            
set smtp-auth-user=2947149465@qq.com   
set smtp-auth-password=???            # 你的邮箱授权码
set smtp-auth=login

3.编写脚本:517564486fdf43088882fe47c72b8459.png

 4.查看结果:627a87ababbf499599ac8f5e441d391e.png

5.编写计划任务: 

[root@server scripts]# crontab -e

0 0 * * * sh /scripts/test1.sh &>/dev/null

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

1.安装httpd包,关闭防火墙和selinux

[root@server ~]# dnf install -y httpd
[root@server ~]# setenforce 0
[root@server ~]# systemctl stop firewalld.service

2.查看进程方式:

1)编写脚本:

[root@server scripts]# vim test2.sh +
total=$(ps -ef | grep httpd | grep -v grep | wc -l)
 
if [ $total -gt 0 ]
then
    echo "hello"
else
    systemctl stop firewalld
    systemctl start httpd
fi

2)加可执行权限,测试:

[root@server scripts]# chmod +x test2.sh 
[root@server scripts]# systemctl stop httpd
[root@server scripts]# ./test2.sh 
[root@server scripts]# ./test2.sh 
hello

3.查看端口方式进行判断:

[root@server scripts]# vim test3.sh +
total=$(netstat -lnupt | grep 80 |wc -l)
 
if [ $total -gt 2 ]
then
    echo "hello"
else
    systemctl stop firewalld
    systemctl start httpd
fi
[root@server scripts]# chmod +x test3.sh 
[root@server scripts]# systemctl stop httpd
[root@server scripts]# ./test3.sh 
[root@server scripts]# ./test3.sh 
hello

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

[root@server scripts]# vim test4.sh +
if curl 192.168.124.132 &>/dev/null
then
    echo "web server is running"
else
    exit 12
fi
[root@server scripts]# chmod +x test4.sh 
[root@server scripts]# systemctl stop httpd
[root@server scripts]# echo $?
12
[root@server scripts]# ./test4.sh 
web server is running

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值