首页 > 系统相关 >Shell阶段04 shell流程之case语句, 服务启动停止脚本(rsync, nginx), shell加锁机制

Shell阶段04 shell流程之case语句, 服务启动停止脚本(rsync, nginx), shell加锁机制

时间:2024-05-21 17:57:46浏览次数:23  
标签:case bin shell 服务 ...... Nginx 加锁 action true

1.流程控制语句之case语句

case主要作用是对程序的选择,循环等操作

#语法:

case 变量 in
    变量值1)
        命令序列
        ;;    #命令序列结束符
    变量值2)
        命令序列
        ;;
    变量值3)
        命令序列
        ;;
    变量值N)
        命令序列
        ;;
    *)            #不符合上面所有条件
        命令序列
        exit    #或者其他命令
esac

#语法示例
安装不同PHP的版本
    1. 菜单, PHP版本的菜单
    2. 提示用户根据菜单进行选择安装不同的PHP版本
    3. 根据用户选择进行安装不同的PHP版本

[root@shell01 scripts]# vim case-1.sh
#!/bin/bash
#1.准备PHP的版本菜单
cat<<EOF
##########################
1.安装PHP-5.5版本
2.安装PHP-5.6版本
3.安装PHP-7.1版本
4.退出当前安装
##########################
EOF
#2.提示用户根据菜单进行选择安装不同的PHP版本
read -p "请根据上方菜单进行选择安装不同的PHP版本[1|2|3|4]: " Install
#3.根据用户的选择,进行安装不同的版本
case $Install in
    1)
        echo "你闲着了安装PHP-5.5版本........."
        echo "正在安装PHP-5.5版本,请稍后......"
        sleep 3    #等待3秒
        echo "PHP-5.5版本安装成功............."
        ;;
    2)
        echo "你闲着了安装PHP-5.6版本........."
        echo "正在安装PHP-5.6版本,请稍后......"
        sleep 3    #等待3秒
        echo "PHP-5.6版本安装成功............."
        ;;
    3)
        echo "你闲着了安装PHP-7.1版本........."
        echo "正在安装PHP-7.1版本,请稍后......"
        sleep 3    #等待3秒
        echo "PHP-7.1版本安装成功............."
        ;;
    4)
        echo "你没有选择安装任何PHP版本! 程序退出!"
        exit
        ;;
    *)
        echo "你的选择不符合要求!请根据上方菜单进行选择[1|2|3|4]."
        exit
esac

 

2.case语句之场景示例(rsync)

服务启动停止脚本

1.编写一个rsync的启动停止脚本
    实现:    start    stop    status    restart
    1.如何启动rsync
    /usr/bin/rsync --daemon
    2.如何停止rsync
    pkill rsync        #注意: 千万不要使用rsync作为脚本的名字
    3.参考系统中其他服务的pid文件,我们自己创建 #(这里不是用system管理,无法用system status去查看)
    
[root@shell01 scripts]# vim case-2.sh
#!/bin/bash
#1.引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断位置变量的个数是否只有一个
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart}"
    exit
fi
#3.编写case语句选项
Pid_File=/var/run/rsync.pid    #这里启动不会自动创建pid文件,需要脚本自行创建
case $1 in
    start)
        if [ -f $Pid_File ];then
            action "服务Rsync正在运行中......" /bin/true
            exit
        else
            #当服务要启动的时候,需要手动创建pid文件
            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 3 #给3秒启动时间
            if [ $? -eq 0 ];then
                action "服务Rsync启动成功....." /bin/true
            else
                action "服务Rsync启动失败....." /bin/false
            fi
        fi
        ;;
    stop)
        if [ -f $Pid_File ];then
            #当服务在启动的状态下,pid文件是存在的,如果需要停止服务,同样需要把pid文件删除
            rm -f $Pid_File && pkill rsync && sleep 2
            if [ $? -eq 0 ];then
                action "服务Rsync停止成功......" /bin/true
            else
                action "服务Rsync停止失败......" /bin/false
            fi
        else
            action "服务Rsync不在运行中......" /bin/true
            exit
        fi
        ;;
    status)
        #当pid文件存在时,则服务在运行中,pid文件不存在时,服务不在运行中
        if [ -f $Pid_File ];then
            action "服务Rsync正在运行中......" /bin/true
        else
            action "服务Rsync不在运行中......" /bin/true
        fi
        ;;
    restart)
        #重启,当服务正在运行中,需要停止服务再启动服务,当服务不在运行中,需要直接启动
        if [ -f $Pid_File ];then
            #停止服务
            rm -f $Pid_File && pkill rsync && sleep 2
            if [ $? -eq 0 ];then
                action "服务Rsync停止成功......" /bin/true
            else
                action "服务Rsync停止失败......" /bin/false
            fi
            #启动服务
            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 3
            if [ $? -eq 0 ];then
                action "服务Rsync启动成功....." /bin/true
            else
                action "服务Rsync启动失败....." /bin/false
            fi
            action "服务Rsync重启成功...." /bin/true
        else
            action "服务Rsync不在运行中......" /bin/true
            #启动服务
            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 3
            if [ $? -eq 0 ];then
                action "服务Rsync启动成功....." /bin/true
            else
                action "服务Rsync启动失败....." /bin/false
            fi
            action "服务Rsync重启成功...." /bin/true
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit
esac

 

3.Nginx的启动停止脚本

start    stop    status      restart        reload

1.怎么启动Nginx
/usr/sbin/nginx
2.怎么停止Nginx
/usr/sbin/nginx -s stop
3.怎么进行重启Nginx
先stop 再start
4.怎么进行平滑重启Nginx
/usr/sbin/nginx -s reload
5.在什么情况下,才能进行平滑重启
    只有在Nginx服务在运行的时候,才能进行平滑重启。如果Nginx服务没有在运行中,是不能进行平滑重启的。
-----------------------------------
[root@shell01 scripts]# vim case-3.sh
#!/bin/bash
#1.引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断位置变量是否存在且只有一个
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit
fi
#3.编写case语句
Pid_File=/var/run/nginx.pid
case $1 in
    start)
        if [ -f $Pid_File ];then
            action "Nginx服务正在运行中......." /bin/true
        else
            /usr/sbin/nginx &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功......" /bin/true
            else
                action "Nginx服务启动失败......" /bin/false
            fi
        fi
        ;;
    stop)
        if [ -f $Pid_File ];then
            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务停止成功......" /bin/true
            else
                action "Nginx服务停止失败......" /bin/false
            fi
        else
            action "Nginx服务不在运行中......" /bin/true
        fi
        ;;
    status)
        if [ -f $Pid_File ];then
            action "Nginx服务正在运行中......" /bin/true
        else
            action "Nginx服务不在运行中......" /bin/true
        fi
        ;;
    restart)
        if [ -f $Pid_File ];then
            #先停止服务
            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务停止成功...." /bin/true
            else
                action "Nginx服务停止失败...." /bin/false
            fi
            #启动服务
            /usr/sbin/nginx &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功......" /bin/true
                action "Nginx服务重启成功......" /bin/true
            else
                action "Nginx服务启动失败......" /bin/false
            fi
        else
            action "Nginx服务不在运行中....." /bin/false
            #启动服务
            /usr/sbin/nginx &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功......" /bin/true
                action "Nginx服务重启成功......" /bin/true
            else
                action "Nginx服务启动失败......" /bin/false
            fi
        fi
        ;;
    reload)
        if [ -f $Pid_File ];then
            /usr/sbin/nginx -s reload &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务平滑重启成功......" /bin/true
            else
                action "Nginx服务平滑重启失败......" /bin/false
            fi
        else
            action "Nginx服务没有在运行中,无法进行平滑重启...." /bin/false
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
        exit
esac
-----------------------------------------

#测试 [root@shell01 scripts]# sh case-3.sh Usage: case-3.sh {start|stop|status|restart|reload} [root@shell01 scripts]# sh case-3.sh start Nginx服务正在运行中....... [ OK ] [root@shell01 scripts]# sh case-3.sh stop Nginx服务停止成功...... [ OK ]

 

4.加锁机制

#加锁机制
当这个脚本被某一个人执行的时候,别人是无法执行的,只能等待执行结束之后,才能执行
#优化脚本(通过创建文件,判断文件是否存在来实现锁的功能)
-----------------------------------------------
[root@shell01 scripts]# vim case-3.sh
#!/bin/bash
#1.引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断位置变量是否存在且只有一个
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit
fi
#2.5 进行加锁机制
Suo_File=/tmp/nginx.lock
if [ -f $Suo_File ];then
    echo "此脚本$0 正在运行中,请稍后再执行......"
    exit
fi
#2.6 加锁
touch $Suo_File &>/dev/null
#3.编写case语句
Pid_File=/var/run/nginx.pid
case $1 in
    start)
        if [ -f $Pid_File ];then
            action "Nginx服务正在运行中......." /bin/true
        else
            /usr/sbin/nginx &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功......" /bin/true
            else
                action "Nginx服务启动失败......" /bin/false
            fi
        fi
        ;;
    stop)
        if [ -f $Pid_File ];then
            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务停止成功......" /bin/true
            else
                action "Nginx服务停止失败......" /bin/false
            fi
        else
            action "Nginx服务不在运行中......" /bin/true
        fi
        ;;
    status)
        if [ -f $Pid_File ];then
            action "Nginx服务正在运行中......" /bin/true
        else
            action "Nginx服务不在运行中......" /bin/true
        fi
        ;;
    restart)
        if [ -f $Pid_File ];then
            #先停止服务
            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务停止成功...." /bin/true
            else
                action "Nginx服务停止失败...." /bin/false
            fi
            #启动服务
            /usr/sbin/nginx &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功......" /bin/true
                action "Nginx服务重启成功......" /bin/true
            else
                action "Nginx服务启动失败......" /bin/false
            fi
        else
            action "Nginx服务不在运行中....." /bin/false
            #启动服务
            /usr/sbin/nginx &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务启动成功......" /bin/true
                action "Nginx服务重启成功......" /bin/true
            else
                action "Nginx服务启动失败......" /bin/false
            fi
        fi
        ;;
    reload)
        if [ -f $Pid_File ];then
            /usr/sbin/nginx -s reload &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "Nginx服务平滑重启成功......" /bin/true
            else
                action "Nginx服务平滑重启失败......" /bin/false
            fi
        else
            action "Nginx服务没有在运行中,无法进行平滑重启...." /bin/false
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
        # 这里删掉exit
esac
#4.解锁(上方不要出现exit)
rm -f $Suo_File &>/dev/null
----------------------------------------

#测试
[root@shell01 scripts]# sh case-3.sh stop
Nginx服务停止成功......                                    [  OK  ]
#在另一个窗口同时执行脚本
[root@shell01 scripts]# sh case-3.sh stop
此脚本case-3.sh 正在运行中,请稍后再执行......

 

标签:case,bin,shell,服务,......,Nginx,加锁,action,true
From: https://www.cnblogs.com/ludingchao/p/18204673

相关文章

  • powershell7升级笔记
    https://learn.microsoft.com/zh-cn/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3#msi安装powershell7法一:推荐MSI安装方式,全打钩,可以自动配置环境变量和右键菜单https://learn.microsoft.com/zh-cn/powershell/scripting/install/i......
  • 在 Windows 上运行,如何以管理员身份运行你的命令提示符或 PowerShell
    在Windows上以管理员身份运行命令提示符或PowerShell是非常简单的过程。以下是具体步骤:以管理员身份运行命令提示符:在开始菜单搜索框中输入cmd,然后从搜索结果中找到并点击命令提示符。右键点击命令提示符图标,然后在弹出的菜单中选择以管理员身份运行。以管理员身份......
  • shell脚本测试
    1、$#:表示执行脚本传入参数的个数2、$*:表示执行脚本传入参数的列表(不包括$0)3、$$:表示进程的id;Shell本身的PID(ProcessID,即脚本运行的当前进程ID号)4、$!:Shell最后运行的后台Process的PID(后台运行的最后一个进程的进程ID号)5、$@:表示获取执行脚本传入的所有参数6、$0:表示执行......
  • Shell编程之免交互
    目录1.HereDocument免交互2.HereDocument常规用法(1)免交互执行命令(2)免交互创建并编辑文件内容(3)tee3.Expect免交互4.编写expect免交互脚本步骤5.read1.HereDocument免交互使用I/O重定向的方式将命令列表提供给交互式程序标准输入的一种替代品命令<<标记...内容 #标记之......
  • java的synchronized有几种加锁方式
    在Java中,synchronized关键字提供了内置的支持来实现同步访问共享资源,以避免并发问题。synchronized主要有三种加锁方式:1.同步实例方法当一个实例方法被声明为synchronized时,该方法将同一时间只能被一个线程访问。锁是当前对象实例(即this)。publicclassSynchronizedInstanceMet......
  • mongo replicaset=rs0 com.mongodb.MongoSocketException: centosc
    1、描述虚拟机搭建mongo副本集虚拟机的设定的hostname为: centosc。虚拟机IP为192.168.25.129搭建三个副本集端口分别为,28017、28018、28019,运行mongo副本集报错,报错信息如下:2024-05-2010:22:39:235[main]INFOorg.apache.coyote.http11.Http11NioProtocol-StartingProtocol......
  • PowerShell脚本,可以用于自动加入或退出域:BAT批处理脚本,可以用于自动加入或退出域:
    PowerShell中创建网页版的自动加入或退出域脚本,您可以使用PowerShellWebAccess(PWA)功能。PWA允许您在Web浏览器中通过安全的HTTPS连接远程运行PowerShell命令,并可与Windows身份验证一起使用。以下是一个示例脚本,可用于创建PWA网页版的自动加入或退出域:powershellCopyCode......
  • 【工具使用】【Shell脚本】【gitlab】下拉所有的仓库代码
    1 前言电脑重置了或者新的项目代码,仓库里二三十个,一个一个拉属实有点拉跨,今儿空了整了个脚本,可以拉下所有的仓库代码。2 前置需要装一个解析json的,windows的话可以直接下载:下载,mac的话可以再官网下载:官网地址。然后加入到PATH下,效果如下:git上新建个access_token,下......
  • SHELL编程
    Shell是一种用户与操作系统内核进行交互的界面。它是命令行解释器,用户通过输入命令,Shell解释并执行这些命令,从而操作系统中的各种功能得以实现。脚本需要以#!/bin/bash开头,并赋予执行权限脚本的执行方式:赋予脚本执行权限chmodu+xhello.sh,然后通过相对路径./hello.sh或通......
  • stm32配合xshell串口输入
    前言通过xshell对stm32f103c8t6芯片进行串口调试。最近发现xshell也可以进行串口调试,但是在数据的输入上会有一些问题。因为正常的串口调试助手都是统一输入,直接发送,但是xshell不同,正常情况下是字符逐一输入的。所以在进行串口调试时,需要逐个字符分析计算,最后统一处理。用xsh......