流程控制之case语句

#基本概述

语法:

case  变量  in

变量1)

命令序列

;;

变量2)

命令序列

;;

变量3)

命令序列

;;

变量N)

命令序列

esac

语法示例:

[root@shell /scripts]# cat case-1.sh

#!/bin/bash

cat<<EOF

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

EOF

read -p "根据上方的菜单,选择安装PHP版本[1|2|3|4]:" Num

case $Num in

    1)

        echo "你选择了安装PHP-5.5版本.............."

        echo "正在安装PHP-5.5版本.................."

        sleep 3

        echo "PHP-5.5版本安装成功.................."

        ;;

    2)

        echo "你选择了安装PHP-5.6版本.............."

        echo "正在安装PHP-5.6版本.................."

        sleep 3

        echo "PHP-5.6版本安装成功.................."

        ;;

    3)

        echo "你选择了安装PHP-7.1版本.............."

        echo "正在安装PHP-7.1版本.................."

        sleep 3

        echo "PHP-7.1版本安装成功.................."

        ;;

    4)

        echo "你没有选择安装任何PHP版本,程序退出!"

        exit

        ;;

    *)

        echo "你的输入不正确!请按照要求输入数字[1|2|3|4]"

        exit

esac

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:1

你选择了安装PHP-5.5版本..............

正在安装PHP-5.5版本..................

PHP-5.5版本安装成功..................

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:2

你选择了安装PHP-5.6版本..............

正在安装PHP-5.6版本..................

PHP-5.6版本安装成功..................

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:3

你选择了安装PHP-7.1版本..............

正在安装PHP-7.1版本..................

PHP-7.1版本安装成功..................

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:4

你没有选择安装任何PHP版本,程序退出!

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:5

你的输入不正确!请按照要求输入数字[1|2|3|4]

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:6

你的输入不正确!请按照要求输入数字[1|2|3|4]

[root@shell /scripts]# sh case-1.sh

##########################

1.安装PHP-5.5版本

2.安装PHP-5.6版本

3.安装PHP-7.1版本

4.退出程序

##########################

根据上方的菜单,选择安装PHP版本[1|2|3|4]:afgfdg

你的输入不正确!请按照要求输入数字[1|2|3|4]

case语句场景示例

#1. 写一个rsync的启动停止脚本,脚本名不要使用rsync命名

怎么启动rsync  /usr/bin/rsync --daemon

怎么停止rsync  pkill rsync

在/var/run/rsync.pid的文件,不存在,当文件存在时,说明服务正在运行中,如果文件则说明服务没有在运行

#使用if去写

[root@shell /scripts]# cat case-2.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart}"

    exit

fi

#3.判断位置变量的值进行比较

State=$1

Pid_File=/var/run/rsync.pid

if [ "$State" == "start" ];then

    if [ -f $Pid_File ];then

        action  "服务Rsync正在运行中........"  /bin/true

    else

        #pid文件不存在时,启动服务,需要手动创建

        touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 2

        if [ $? -eq 0 ];then

            action "服务Rsync启动成功........." /bin/true

        else

            action "服务Rsync启动失败........." /bin/false

            exit

        fi

    fi

elif [ "$State" == "stop" ];then

    if [ -f $Pid_File ];then

        #如果停止服务,文件存在时,一定要删除

        rm -f  $Pid_File  && pkill rsync &>/dev/null && sleep 2

        if [ $? -eq 0 ];then

            action "服务Rsync停止成功........." /bin/true

        else

            action "服务Rsync停止失败........." /bin/false

            exit

        fi

    else

        action "服务Rsync不在运行中..........." /bin/true

    fi

elif [ "$State" == "status" ];then

    if [ -f $Pid_File ];then

        action "服务Rsync正在运行中..........." /bin/true

    else

        action "服务Rsync不在运行中..........." /bin/true

    fi

elif [ "$State" == "restart" ];then

    if [ -f $Pid_File ];then

        rm -f  $Pid_File  && pkill rsync &>/dev/null && 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 2

        if [ $? -eq 0 ];then

            action "服务Rsync启动成功........." /bin/true

        else

            action "服务Rsync启动失败........." /bin/false

        fi

    else

        action "服务Rsync不在运行中..........." /bin/true

        touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null  && sleep 2

        if [ $? -eq 0 ];then

            action "服务Rsync启动成功........." /bin/true

        else

            action "服务Rsync启动失败........." /bin/false

        fi

    fi

else

    echo "Usage: $0 {start|stop|status|restart}"

    exit

fi

#使用case语句写

[root@shell /scripts]# cat case-3.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart}"

    exit

fi

#3.判断位置变量的值进行比较

State=$1

Pid_File=/var/run/rsync.pid

case $State in

    start)

        if [ -f $Pid_File ];then

            action  "服务Rsync正在运行中........"  /bin/true

        else

            #pid文件不存在时,启动服务,需要手动创建

            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 2

            if [ $? -eq 0 ];then

                action "服务Rsync启动成功........." /bin/true

            else

                action "服务Rsync启动失败........." /bin/false

                exit

            fi

        fi

        ;;

    stop)

        if [ -f $Pid_File ];then

            #如果停止服务,文件存在时,一定要删除

            rm -f  $Pid_File  && pkill rsync &>/dev/null && sleep 2

            if [ $? -eq 0 ];then

                action "服务Rsync停止成功........." /bin/true

            else

                action "服务Rsync停止失败........." /bin/false

                exit

            fi

        else

            action "服务Rsync不在运行中..........." /bin/true

        fi

        ;;

    status)

        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 &>/dev/null && 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 2

            if [ $? -eq 0 ];then

                action "服务Rsync启动成功........." /bin/true

            else

                action "服务Rsync启动失败........." /bin/false

            fi

        else

            action "服务Rsync不在运行中..........." /bin/true

            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null  && sleep 2

            if [ $? -eq 0 ];then

                action "服务Rsync启动成功........." /bin/true

            else

                action "服务Rsync启动失败........." /bin/false

            fi

        fi

        ;;

    *)

        echo "Usage: $0 {start|stop|status|restart}"

        exit

esac

[root@shell /scripts]# sh case-3.sh status

服务Rsync正在运行中...........                            [  OK  ]

[root@shell /scripts]# sh case-3.sh start

服务Rsync正在运行中........                                [  OK  ]

[root@shell /scripts]# sh case-3.sh start

服务Rsync正在运行中........                                [  OK  ]

[root@shell /scripts]# sh case-3.sh stop

服务Rsync停止成功.........                                [  OK  ]

[root@shell /scripts]# ps aux| grep rsync | grep -v grep

[root@shell /scripts]# sh case-3.sh  restart

服务Rsync不在运行中...........                            [  OK  ]

服务Rsync启动成功.........                                [  OK  ]

[root@shell /scripts]# ps aux| grep rsync | grep -v grep

root      20109  0.0  0.0 114744  360 ?        Ss  17:19  0:00 /usr/bin/rsync --daemon

[root@shell /scripts]#

#2. 写一个nginx的启动停止脚本

nginx如何启动  /usr/sbin/nginx

nginx如何停止 /usr/sbin/nginx -s stop

nginx的pid文件  /var/run/nginx.pid

reload平滑重启 如何进行平滑重启 /usr/sbin/nginx -s reload 

在什么情况下,才能进行平滑重启  只有在nginx服务在运行中的时候,才能进行平滑重启,如果nginx服务没有在运行中,是无法进行平滑重启的。

start  stop  status  restart  reload

[root@shell /scripts]# cat case-4.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart|reload}"

    exit

fi

#3.写case语句变量体

State=$1

Pid_File=/var/run/nginx.pid

case $State 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

                else

                    action "服务Nginx启动失败..............." /bin/false

                fi

            else

                action "服务Nginx不在运行中................." /bin/true

                /usr/sbin/nginx &>/dev/null && sleep 2

                if [ $? -eq 0 ];then                                                                                                                       

                    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@shell /scripts]# sh case-4.sh status

服务Nginx正在运行中.................                      [  OK  ]

[root@shell /scripts]# ps aux| grep nginx

root      20143  0.0  0.1 120784  2104 ?        Ss  17:23  0:00 nginx: master process /usr/sbin/nginx

nginx    20144  0.0  0.1 121180  3132 ?        S    17:23  0:00 nginx: worker process

root      20247  0.0  0.0 112708  976 pts/0    R+  18:03  0:00 grep --color=auto nginx

[root@shell /scripts]# sh case-4.sh start

服务Nginx正在运行中................                        [  OK  ]

[root@shell /scripts]# sh case-4.sh restart

服务Nginx停止成功...............                          [  OK  ]

服务Nginx启动成功...............                          [  OK  ]

[root@shell /scripts]# ps aux| grep nginx

root      20263  0.0  0.1 120784  2104 ?        Ss  18:03  0:00 nginx: master process /usr/sbin/nginx

nginx    20265  0.0  0.1 121180  3132 ?        S    18:03  0:00 nginx: worker process

root      20268  0.0  0.0 112708  976 pts/0    R+  18:03  0:00 grep --color=auto nginx

[root@shell /scripts]# sh case-4.sh stop

服务Nginx停止成功...............                          [  OK  ]

[root@shell /scripts]# sh case-4.sh stop

服务Nginx不在运行中.................                      [  OK  ]

[root@shell /scripts]# sh case-4.sh reload

服务Nginx不在运行中,无法进行平滑重启操作......            [FAILED]

[root@shell /scripts]# sh case-4.sh start

服务Nginx启动成功...............                          [  OK  ]

[root@shell /scripts]# sh case-4.sh reload

服务Nginx平滑重启成功..........                            [  OK  ]

#加锁机制

[root@shell /scripts]# cat case-4.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#1.0 加锁机制

Suo=/tmp/nginx.lock

if [ -f $Suo ];then

    echo "此脚本$0 正在运行中,请稍后再执行........."

    exit

fi

#创建锁

touch $Suo &>/dev/null

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart|reload}"

    exit

fi

#3.写case语句变量体

State=$1

Pid_File=/var/run/nginx.pid

case $State 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

                else

                    action "服务Nginx启动失败..............." /bin/false

                fi

            else

                action "服务Nginx不在运行中................." /bin/true

                /usr/sbin/nginx &>/dev/null && sleep 2

                if [ $? -eq 0 ];then                                                                                                                       

                    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

#解锁

rm -f $Suo &>/dev/null

[root@shell /scripts]#

[root@shell /scripts]# sh case-4.sh status

服务Nginx正在运行中.................                      [  OK  ]

[root@shell /scripts]# sh case-4.sh restart

服务Nginx停止成功...............                          [  OK  ]

服务Nginx启动成功...............                          [  OK  ]

[root@shell /scripts]#

#另一个窗口同时执行

[root@shell ~]# sh /scripts/case-4.sh  start

此脚本/scripts/case-4.sh 正在运行中,请稍后再执行.........

[root@shell ~]# sh /scripts/case-4.sh  start

服务Nginx正在运行中................                        [  OK  ]

#1. 写一个rsync的启动停止脚本,脚本名不要使用rsync命名

怎么启动rsync  /usr/bin/rsync --daemon

怎么停止rsync  pkill rsync

在/var/run/rsync.pid的文件,不存在,当文件存在时,说明服务正在运行中,如果文件则说明服务没有在运行

#使用if去写

[root@shell /scripts]# cat case-2.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart}"

    exit

fi

#3.判断位置变量的值进行比较

State=$1

Pid_File=/var/run/rsync.pid

if [ "$State" == "start" ];then

    if [ -f $Pid_File ];then

        action  "服务Rsync正在运行中........"  /bin/true

    else

        #pid文件不存在时,启动服务,需要手动创建

        touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 2

        if [ $? -eq 0 ];then

            action "服务Rsync启动成功........." /bin/true

        else

            action "服务Rsync启动失败........." /bin/false

            exit

        fi

    fi

elif [ "$State" == "stop" ];then

    if [ -f $Pid_File ];then

        #如果停止服务,文件存在时,一定要删除

        rm -f  $Pid_File  && pkill rsync &>/dev/null && sleep 2

        if [ $? -eq 0 ];then

            action "服务Rsync停止成功........." /bin/true

        else

            action "服务Rsync停止失败........." /bin/false

            exit

        fi

    else

        action "服务Rsync不在运行中..........." /bin/true

    fi

elif [ "$State" == "status" ];then

    if [ -f $Pid_File ];then

        action "服务Rsync正在运行中..........." /bin/true

    else

        action "服务Rsync不在运行中..........." /bin/true

    fi

elif [ "$State" == "restart" ];then

    if [ -f $Pid_File ];then

        rm -f  $Pid_File  && pkill rsync &>/dev/null && 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 2

        if [ $? -eq 0 ];then

            action "服务Rsync启动成功........." /bin/true

        else

            action "服务Rsync启动失败........." /bin/false

        fi

    else

        action "服务Rsync不在运行中..........." /bin/true

        touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null  && sleep 2

        if [ $? -eq 0 ];then

            action "服务Rsync启动成功........." /bin/true

        else

            action "服务Rsync启动失败........." /bin/false

        fi

    fi

else

    echo "Usage: $0 {start|stop|status|restart}"

    exit

fi

#使用case语句写

[root@shell /scripts]# cat case-3.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart}"

    exit

fi

#3.判断位置变量的值进行比较

State=$1

Pid_File=/var/run/rsync.pid

case $State in

    start)

        if [ -f $Pid_File ];then

            action  "服务Rsync正在运行中........"  /bin/true

        else

            #pid文件不存在时,启动服务,需要手动创建

            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null && sleep 2

            if [ $? -eq 0 ];then

                action "服务Rsync启动成功........." /bin/true

            else

                action "服务Rsync启动失败........." /bin/false

                exit

            fi

        fi

        ;;

    stop)

        if [ -f $Pid_File ];then

            #如果停止服务,文件存在时,一定要删除

            rm -f  $Pid_File  && pkill rsync &>/dev/null && sleep 2

            if [ $? -eq 0 ];then

                action "服务Rsync停止成功........." /bin/true

            else

                action "服务Rsync停止失败........." /bin/false

                exit

            fi

        else

            action "服务Rsync不在运行中..........." /bin/true

        fi

        ;;

    status)

        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 &>/dev/null && 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 2

            if [ $? -eq 0 ];then

                action "服务Rsync启动成功........." /bin/true

            else

                action "服务Rsync启动失败........." /bin/false

            fi

        else

            action "服务Rsync不在运行中..........." /bin/true

            touch $Pid_File && /usr/bin/rsync --daemon &>/dev/null  && sleep 2

            if [ $? -eq 0 ];then

                action "服务Rsync启动成功........." /bin/true

            else

                action "服务Rsync启动失败........." /bin/false

            fi

        fi

        ;;

    *)

        echo "Usage: $0 {start|stop|status|restart}"

        exit

esac

[root@shell /scripts]# sh case-3.sh status

服务Rsync正在运行中...........                            [  OK  ]

[root@shell /scripts]# sh case-3.sh start

服务Rsync正在运行中........                                [  OK  ]

[root@shell /scripts]# sh case-3.sh start

服务Rsync正在运行中........                                [  OK  ]

[root@shell /scripts]# sh case-3.sh stop

服务Rsync停止成功.........                                [  OK  ]

[root@shell /scripts]# ps aux| grep rsync | grep -v grep

[root@shell /scripts]# sh case-3.sh  restart

服务Rsync不在运行中...........                            [  OK  ]

服务Rsync启动成功.........                                [  OK  ]

[root@shell /scripts]# ps aux| grep rsync | grep -v grep

root      20109  0.0  0.0 114744  360 ?        Ss  17:19  0:00 /usr/bin/rsync --daemon

[root@shell /scripts]#

#2. 写一个nginx的启动停止脚本

nginx如何启动  /usr/sbin/nginx

nginx如何停止 /usr/sbin/nginx -s stop

nginx的pid文件  /var/run/nginx.pid

reload平滑重启 如何进行平滑重启 /usr/sbin/nginx -s reload 

在什么情况下,才能进行平滑重启  只有在nginx服务在运行中的时候,才能进行平滑重启,如果nginx服务没有在运行中,是无法进行平滑重启的。

start  stop  status  restart  reload

[root@shell /scripts]# cat case-4.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart|reload}"

    exit

fi

#3.写case语句变量体

State=$1

Pid_File=/var/run/nginx.pid

case $State 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

                else

                    action "服务Nginx启动失败..............." /bin/false

                fi

            else

                action "服务Nginx不在运行中................." /bin/true

                /usr/sbin/nginx &>/dev/null && sleep 2

                if [ $? -eq 0 ];then                                                                                                                       

                    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@shell /scripts]# sh case-4.sh status

服务Nginx正在运行中.................                      [  OK  ]

[root@shell /scripts]# ps aux| grep nginx

root      20143  0.0  0.1 120784  2104 ?        Ss  17:23  0:00 nginx: master process /usr/sbin/nginx

nginx    20144  0.0  0.1 121180  3132 ?        S    17:23  0:00 nginx: worker process

root      20247  0.0  0.0 112708  976 pts/0    R+  18:03  0:00 grep --color=auto nginx

[root@shell /scripts]# sh case-4.sh start

服务Nginx正在运行中................                        [  OK  ]

[root@shell /scripts]# sh case-4.sh restart

服务Nginx停止成功...............                          [  OK  ]

服务Nginx启动成功...............                          [  OK  ]

[root@shell /scripts]# ps aux| grep nginx

root      20263  0.0  0.1 120784  2104 ?        Ss  18:03  0:00 nginx: master process /usr/sbin/nginx

nginx    20265  0.0  0.1 121180  3132 ?        S    18:03  0:00 nginx: worker process

root      20268  0.0  0.0 112708  976 pts/0    R+  18:03  0:00 grep --color=auto nginx

[root@shell /scripts]# sh case-4.sh stop

服务Nginx停止成功...............                          [  OK  ]

[root@shell /scripts]# sh case-4.sh stop

服务Nginx不在运行中.................                      [  OK  ]

[root@shell /scripts]# sh case-4.sh reload

服务Nginx不在运行中,无法进行平滑重启操作......            [FAILED]

[root@shell /scripts]# sh case-4.sh start

服务Nginx启动成功...............                          [  OK  ]

[root@shell /scripts]# sh case-4.sh reload

服务Nginx平滑重启成功..........                            [  OK  ]

#加锁机制

[root@shell /scripts]# cat case-4.sh

#!/bin/bash

#1.调用函数库

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#1.0 加锁机制

Suo=/tmp/nginx.lock

if [ -f $Suo ];then

    echo "此脚本$0 正在运行中,请稍后再执行........."

    exit

fi

#创建锁

touch $Suo &>/dev/null

#2. 判断执行脚本时是否只有一个位置变量

if [ $# -ne 1 ];then

    echo "Usage: $0 {start|stop|status|restart|reload}"

    exit

fi

#3.写case语句变量体

State=$1

Pid_File=/var/run/nginx.pid

case $State 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

                else

                    action "服务Nginx启动失败..............." /bin/false

                fi

            else

                action "服务Nginx不在运行中................." /bin/true

                /usr/sbin/nginx &>/dev/null && sleep 2

                if [ $? -eq 0 ];then                                                                                                                       

                    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

#解锁

rm -f $Suo &>/dev/null

[root@shell /scripts]#

[root@shell /scripts]# sh case-4.sh status

服务Nginx正在运行中.................                      [  OK  ]

[root@shell /scripts]# sh case-4.sh restart

服务Nginx停止成功...............                          [  OK  ]

服务Nginx启动成功...............                          [  OK  ]

[root@shell /scripts]#

#另一个窗口同时执行

[root@shell ~]# sh /scripts/case-4.sh  start

此脚本/scripts/case-4.sh 正在运行中,请稍后再执行.........

[root@shell ~]# sh /scripts/case-4.sh  start

服务Nginx正在运行中................                        [  OK  ]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271