八、Shell之if语句实战

案例1:开发Shell脚本判断系统剩余内存的大小,如果低于2000MB,就邮件报警给系统管理员,并且将脚本加入系统定时任务,即每3分钟执行一次检查。

1)获取当前系统剩余内存的值
[root@node1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1999         120        1527           8         351        1685
Swap:          4095           0        4095
#取avaliable的值
[root@node1 ~]# free -m|awk 'NR==2{print $NF}'
1685

2)配置邮件报警(可采用第三方邮件服务器)
发邮件的客户端常见的有mail或mutt;服务端有sendmail服务(CentOS 5下默认的)、postfix服务(CentOS 6下默认的)。这里不使用本地的邮件服务,而是使用本地的mail客户端,以及第三方的邮件服务商,例如:163.com(需要提前注册用户),利用这个邮件账号来给接收报警的人发送报警邮件。
[root@node1 ~]#echo -e "set from=yaowangxi@163.com smtp=smtp.163.com\nset smtp-auth-user=yaowangxi@163.com smtp-auth-password=VCASBMTUKXOGKHAL smtp-auth=login" >>/etc/mail.rc  

#set from=yaowangxi@163.com   # 发件人
#set smtp=smtp.163.com  # smtp
#set smtp-auth=login
#set smtp-auth-user=yaowangxi@163.com   # smtp认证邮箱
#set smtp-auth-password=VCASBMTUKXOGKHAL  # 注意现在163邮箱密码并不是网页登录密码,是一个专用的SMTP密码
[root@node1 ~]# systemctl restart postfix.service 
[root@node1 ~]# echo test |mail -s "test" yaowangxi@163.com

3)编写Shell脚本。

#! /bin/bash
MEM=$(free -m |awk 'NR==2{print $NF}') #取剩余可用的MEM值
if [ $MEM -le 2000 ];then
   echo "$MEM less than 2000M" |tee /tmp/mem_log
   echo "$(date +%F-%T)_system_mem is $MEM, less 2000M"|mail -s "MEM warry" yaowangxi@163.com
fi
#MEM小于2000M则发送警告并给yaowangxi@163.com发送邮件

测试脚本:

[root@node1 scripts]# bash check_mem.sh 
1681 less than 2000M

4)把脚本加入定时任务
[root@node1 scripts]# crontab -l
*/3 * * * * /usr/bin/bash /scripts/check_mem.sh

[root@node1 scripts]# tailf /var/log/cron
Oct 10 00:01:01 node1 CROND[24146]: (root) CMD (run-parts /etc/cron.hourly)
Oct 10 00:01:01 node1 run-parts(/etc/cron.hourly)[24146]: starting 0anacron
Oct 10 00:01:01 node1 anacron[24155]: Anacron started on 2020-10-10
Oct 10 00:01:01 node1 anacron[24155]: Normal exit (0 jobs run)
Oct 10 00:01:01 node1 run-parts(/etc/cron.hourly)[24157]: finished 0anacron
Oct 10 00:49:32 node1 crontab[26801]: (root) BEGIN EDIT (root)
Oct 10 00:50:25 node1 crontab[26801]: (root) REPLACE (root)
Oct 10 00:50:25 node1 crontab[26801]: (root) END EDIT (root)
Oct 10 00:50:29 node1 crontab[26854]: (root) LIST (root)
Oct 10 00:51:01 node1 CROND[26904]: (root) CMD (/usr/bin/bash /scripts/check_mem.sh)
Oct 10 00:54:01 node1 CROND[27077]: (root) CMD (/usr/bin/bash /scripts/check_mem.sh)
Oct 10 00:57:01 node1 CROND[27238]: (root) CMD (/usr/bin/bash /scripts/check_mem.sh)
#定时任务在正常运行

案例2:分别使用read读入及脚本传参的方式比较两个整数的大小。

方法一:使用单分支

#! /bin/bash

#把$1赋值给a,把$2赋值给b
a=$1
b=$2

#判断脚本后紧跟了2个参数$1 $2
if [ $# -ne 2 ];then
   echo "Usage: $0 age1 age2!"
   exit 0
fi

#判断$1和$2为正整数
expr $a + $b + 10 > /dev/null

if [ $? -ne 0 ];then
   echo "Please enter 2 positive integers!"
   exit 0
fi

#单分支比较2个数的大小
if [ $a -gt $b ];then
   echo "$a > $b"
fi

if [ $a -lt $b ];then
   echo "$a < $b"
fi

if [ $a -eq $b ];then
   echo "$a = $b"
fi
                

方法二:if多分支比较

#! /bin/bash
#把$1赋值给a,把$2赋值给b
a=$1
b=$2

#判断脚本后紧跟了2个参数$1 $2
if [ $# -ne 2 ];then
   echo "Usage: $0 age1 age2!"
   exit 0
fi

#判断$1和$2为正整数
expr $a + $b + 10 &> /dev/null

if [ $? -ne 0 ];then
   echo "Please enter 2 positive integers!"
   exit 0
fi

#多分支比较2个数的大小
if [ $a -gt $b ];then
   echo "$a > $b"
elif [ $a -lt $b ];then
   echo "$a < $b"
else
   echo "$a = $b"
fi
                

脚本测试:

[root@node1 scripts]# bash bijiaodaxiao.sh 
Usage: bijiaodaxiao.sh age1 age2!
[root@node1 scripts]# bash bijiaodaxiao.sh  10 20
10 < 20
[root@node1 scripts]# bash bijiaodaxiao.sh  100 20
100 > 20
[root@node1 scripts]# bash bijiaodaxiao.sh  100 100
100 = 100
[root@node1 scripts]# bash bijiaodaxiao.sh  100 ads
expr: non-integer argument
Please enter 2 positive integers!
[root@node1 scripts]# 

案例3:监控nginx是否正常运行

该案例的nginx使用yum安装

[root@node1 scripts]# yum install -y nginx
#启动nginx
[root@node1 scripts]# systemctl start nginx
[root@node1 scripts]# pidof nginx
15694 15693

[root@node1 scripts]# netstat -antp|grep ":80"
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15693/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      15693/nginx: master 
#! /bin/bash

#判断nginx是否正常运行,pidof nginx|wc -l来判断,值为0,则nginx没有运行需要重启nginx服务,不为0,nginx工作正常
NGX_NUM1=$(pidof nginx |wc -l)
if [ $NGX_NUM1 -eq 0 ];then
   systemctl stop nginx &> /dev/null
   sleep 3
   systemctl start nginx &>/dev/null
   #重启后nginx是否正常运行,正常则打印"nginx server restart ok!",不正常则打印"Plesae check nginx server worry!"
   NGX_NUM2=$(pidof nginx |wc -l)
   if [ $NGX_NUM2 -ne 0 ];then
       echo "nginx server restart ok!"
   else
       echo "Plesae check nginx server worry!"
   fi
else
   echo "nginx running !"
fi

脚本测试:

1、nginx工作正常
[root@node1 scripts]# ps -ef |grep nginx
root     24333     1  0 22:09 ?        00:00:00 nginx: master process nginx
nginx    24334 24333  0 22:09 ?        00:00:00 nginx: worker process
root     24345 14794  0 22:09 pts/0    00:00:00 grep --color=auto nginx
[root@node1 scripts]# pidof nginx
24334 24333

[root@node1 scripts]# bash nginx_check.sh 
nginx running !

2、停止nginx
[root@node1 scripts]# nginx -s stop
[root@node1 scripts]# pidof nginx
[root@node1 scripts]# ps -ef |grep nginx
root     24400 14794  0 22:10 pts/0    00:00:00 grep --color=auto nginx

[root@node1 scripts]# bash nginx_check.sh 
nginx server restart ok!
[root@node1 scripts]# ps -ef |grep nginx
root     24439     1  0 22:10 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx    24440 24439  0 22:10 ?        00:00:00 nginx: worker process
root     24449 14794  0 22:10 pts/0    00:00:00 grep --color=auto nginx
[root@node1 scripts]# pidof nginx
24440 24439
#nginx重启成功

3、输出nginx
[root@node1 scripts]# yum remove -y nginx
[root@node1 scripts]# bash nginx_check.sh 
Plesae check nginx server worry!
#提示nginx重启失败,需要手动处理

 

posted @ 2020-10-10 14:23  yaowx  阅读(243)  评论(0编辑  收藏  举报