ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

超详细shell脚本小练...

超详细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 ];thenecho "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 ]
thenecho "nginx is stopping, please waiting..."yum install nginx -y &> /dev/nullsystemctl start firewalldsystemctl start nginxfirewall-cmd --permanent --zone=public --add-server=http &> /dev/nullfirewall-cmd --permanent --zone=public --add-port=80 &> /dev/nullfirewall-cmd --reload > /dev/nullecho "nginx is running"
elseecho "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..."
elseecho "web is not running..."exit 12
fi

3.2.测试

 

返回列表