超详细shell脚本小练...

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

1.1.安装邮件服务

 

[root@node ~]# df -m
文件系统                   1M-块  已用  可用 已用% 挂载点
devtmpfs                     700     0   700    0% /dev
tmpfs                        716     0   716    0% /dev/shm
tmpfs                        287     8   279    3% /run
tmpfs                          4     0     4    0% /sys/fs/cgroup
/dev/mapper/openeuler-root 27385  2056 23913    8% /
tmpfs                        716     0   716    0% /tmp
/dev/sda1                    920    87   770   11% /boot
[root@node ~]# yum install mailx -y
[root@node ~]# vim /etc/mail.rc
set from=3471521632@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=3471521632@qq.com
set smtp-auth-password=anfbowpmrzfdchhi
set smtp-auth=login
注:使用:wq!强制退出

1.2.编写脚本

#!/bin/bash
disk=$(df -m | grep -w "/" | tr -s " " | cut -d " " -f4)
if [ $disk -lt 30000 ];then
	echo "warning:disk space less than 30G!!!" | mail -s "warning:disk space less than 30G!!!" 3471521632@qq.com
fi

注:df -m 以M为单位显示
    grep -w 精准定位
    tr -s 压缩空格

1.3.系统计划任务

0 0 * * *  root /bin/bash /root/df.sh

1.4.测试

发现时间不对 ,没有同步

[root@node ~]# timedatectl
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no
[root@node ~]# systemctl start  chronyd
[root@node ~]# timedatectl
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

 再次测试


2、判断web服务是否运行

(1、査看进程的方式判断该程序是否运行2、通过查看端口的方式判断该程序是否运行)

如果没有运行,则启动该服务并配置防火墙规则。

2.1.编写脚本

(脚本名字不要含nginx,否则grep有误)

[root@server ~]# cat ng.sh
#!/bin/bash
ps=$(ps -ef | grep nginx | grep -v grep | wc -l)
#ps=$(netstat -lntup | grep -w 80 | wc -l)
#ps=$(ss -lntup | grep -w 80 | wc -l)
if [ "$ps" -eq 0 ]
then
        echo "nginx is stopping, please waiting..."
        yum install nginx -y &> /dev/null
        systemctl start firewalld
	    systemctl start nginx
        firewall-cmd --permanent --zone=public --add-server=http &> /dev/null
        firewall-cmd --permanent --zone=public --add-port=80 &> /dev/null
        firewall-cmd --reload > /dev/null
        echo "nginx is running"
else
	echo "nginx is already running..."
fi

 2.2.测试

[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disa
     Active: active (running) since Thu 2024-01-25 03:14:12 CST; 3mi
   

[root@server ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled
     Active: active (running) since Thu 2024-01-25 03:18:48 CST; 16s

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

3.1.编写脚本

#!/bin/bash
ip=$(ip a | grep ens32 | grep inet | tr -s " " | cut -d " " -f3 | cut -d "/" -f1)
curl -s $ip &> $ip
if [ $? -eq 0 ]
then 
	echo "web-server is running..."
else
	echo "web is not running..."
	exit 12
fi

3.2.测试

 

  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁小木在努力冲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值