Linux基础-Shell脚本

Posted DragonFire

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux基础-Shell脚本相关的知识,希望对你有一定的参考价值。

任务一目标:自动部署、初始配置、并启动nginx反向代理服务

把任务拆分来看-自动部署部分,就是先下载安装Nginx

首先建立一个很NB霸气的目录还有一个同样NB霸气的.sh文件

/NBshell/MissionOne.sh

自动化部署代码如下:

#/bin/bash
systemctl status nginx
if(($?==4))
        then
        yum install -y nginx
        if(($?==0))
                then
                #echo \'Yes!\'
                systemctl start nginx
                if(($?==0))
                        then
                        echo "Congratulations!! Nginx start OK!!"
                else
                        echo "Sorry is Fail!!!" 
                fi
        else
                echo"sorry install is Fail!!!"

        fi
elif(($?==3))
        then
        systemctl start nginx
        if(($?==0))
                then
                echo "Congratulations!! Nginx start OK!!!"
        else
                echo "sorry!!"
        fi
elif(($?==0))
        then
        echo "OKOKOK!!!"
else
        echo "I am so sorry"    
fi

检测系统是否安装nginx如果没有,就安装nginx,并启动

如果已安装未启动,就启动,如果已启动,就万事大吉了!

其次是初始化配置成反向代理,也就是对/etc/nginx/nginx.config进行编辑

使用sed流式编辑工具

 1 echo "config writing...."
 2 sed -ri \'/^http/a upstream Yanlong {\' /etc/nginx/nginx.conf
 3 sed -ri \'/^upst/a server yanlongweb1\\;\' /etc/nginx/nginx.conf
 4 sed -ri \'/^server yanlongweb1/a server yanlongweb2\\;\' /etc/nginx/nginx.conf
 5 sed -ri \'/^server yanlongweb2/a \\}\' /etc/nginx/nginx.conf
 6 sed -ri \'/^(\\ +)(location)(\\ )(\\/)/a proxy_pass http:\\/\\/Yanlong\\;\' /etc/nginx/nginx.conf
 7 echo"config write is OK!"
 8 systemctl reload nginx
 9 if(($?==0))
10         then
11         echo "HTTP load balancer is OK!"
12 else
13         echo "Sorry!!"
14 fi

脚本执行成功!服务器配置完成!

任务二目标:自动部署、初始配置、并启动三台web

由于物理环境的限制,只能开启两台WEB服务器分别为yanlongweb1和yanlongweb2

并执行以下脚本对nginx进行安装部署配置,并启动

#/bin/bash
systemctl status nginx
if(($?==4))
        then
        yum install -y nginx
        if(($?==0))
                then
                #echo \'Yes!\'
                systemctl start nginx
                if(($?==0))
                        then
                        echo "Congratulations!! Nginx start OK!!"
                else
                        echo "Sorry is Fail!!!" 
                fi
        else
                echo"sorry install is Fail!!!"

        fi
elif(($?==3))
        then
        systemctl start nginx
        if(($?==0))
                then
                echo "Congratulations!! Nginx start OK!!!"
        else
                echo "sorry!!"
        fi
elif(($?==0))
        then
        echo "OKOKOK!!!"
else
        echo "I am so sorry"    
fi
echo "config writing...."
sed -ri \'/^(\\ +)(location)(\\ )(\\/)/a root\\ \\/nginxwebservice\\;\' /etc/nginx/nginx.conf
sed -ri \'/^root\\ \\/nginxwebservice/a index\\ web.html\\;\' /etc/nginx/nginx.conf
echo "config write is OK!"
systemctl reload nginx
if(($?==0))
        then
        echo "HTTP load balancer YanlongWEBservice is OK!"
else
        echo "Sorry!!"
fi

脚本执行完成,检查服务启动状态

现在进行访问web1

访问web2

 

任务一中搭建的反向代理:

访问反向代理服务器:192.168.16.119

脚本执行成功!服务器搭建成功

任务三目标:监控脚本:监控每台机器的内存使用率>70%,则输出报警信息

#/bin/bash
bu=`free | awk \'NR==2{print $6}\'`
to=`free | awk \'NR==2{print $2}\'`
mem=`expr "scale=2;$bu/$to" |bc -l | cut -d. -f2`
while true
do
        if(($mem >= 70))
                then
                echo "目前内存使用率是:${mem}%"
                sleep 1
        fi
done

后台运行./LookMem.sh &

为了实现效果,我将内存的报警阈值调为15%

请看效果 

 

以上是关于Linux基础-Shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

Linux-shell脚本基础

linux学习笔记--工程师技术:shell脚本基础

Shell脚本编程基础——Linux基本命令(11)

Linux基础-Shell脚本

Linux运维之道之ENGINEER1.4(shell脚本基础)

代码片段:Shell脚本实现重复执行和多进程