脚本一:

[root@shell /service/scripts/day05]# cat case-3.sh
#!/bin/bash
#引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#判断执行脚本时的位置变量是否存在且唯一
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|reload|restart}"
    exit
fi
Pid_File=/var/run/nginx.pid
#编写case语句
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
            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|reload|restart}"
esac

[root@shell /service/scripts/day05]# sh case-3.sh stop
服务Nginx停止成功....                                      [  OK  ]
[root@shell /service/scripts/day05]# sh case-3.sh status
服务Nginx不在运行中....                                    [  OK  ]
[root@shell /service/scripts/day05]# sh case-3.sh restart
服务Nginx不在运行中....                                    [  OK  ]
服务Nginx启动成功....                                      [  OK  ]
[root@shell /service/scripts/day05]# sh case-3.sh restart
服务Nginx停止成功....                                      [  OK  ]
服务Nginx启动成功....                                      [  OK  ]
[root@shell /service/scripts/day05]# sh case-3.sh reload
服务Nginx平滑重启成功....                                  [  OK  ]
[root@shell /service/scripts/day05]# sh case-3.sh stop
服务Nginx停止成功....                                      [  OK  ]
[root@shell /service/scripts/day05]# sh case-3.sh reload
服务Nginx不在运行中!无法进行平滑重启操作....              [FAILED]

脚本二:加锁机制

[root@shell /service/scripts/day05]# cat case-3.sh
#!/bin/bash
#引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#加锁
Suo=/tmp/nginx.lock
#判断是否存在锁机制
if [ -f $Suo ];then
    echo "此脚本${0}正在运行中!请稍后再执行...."
    exit
fi
#判断执行脚本时的位置变量是否存在且唯一
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|reload|restart}"
    exit
fi
#创建锁
touch  $Suo
Pid_File=/var/run/nginx.pid
#编写case语句
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
            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|reload|restart}"
esac

#解锁
rm -f $Suo
[root@shell /service/scripts/day05]# sh case-3.sh restart
此脚本case-3.sh正在运行中!请稍后再执行....

脚本三:检查语法交互式修改

#加强版的nginx启动脚本 
[root@shell /service/scripts/day05]# cat case-4.sh
#!/bin/bash
#引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#加锁
Suo=/tmp/nginx.lock
#判断是否存在锁机制
if [ -f $Suo ];then
    echo "此脚本${0}正在运行中!请稍后再执行...."
    exit
fi
#判断执行脚本时的位置变量是否存在且唯一
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|reload|restart}"
    exit
fi
#创建锁
touch  $Suo
Pid_File=/var/run/nginx.pid
#编写case语句
case $1 in
    start)
        if [ -f $Pid_File ];then
            action "服务Nginx正在运行中...."  /bin/true
        else
            /usr/sbin/nginx -t &>/dev/null
            if [ $? -eq 0 ];then
                echo "Nginx语法检查成功...."
                /usr/sbin/nginx &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "服务Nginx启动成功...." /bin/true
                else
                    action "服务Nginx启动失败...." /bin/false
                fi
            else
                Error=/tmp/nginx_error.log
                /usr/sbin/nginx -t &>$Error
                Nginx_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
                Nginx_Line=$(awk  -F '[ :]' 'NR==1{print $NF}' $Error)
                /usr/sbin/nginx -t
                read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
                case $Confirm in
                    Yes|y|Y)
                        vim +$Nginx_Line  $Nginx_File
                        /usr/sbin/nginx -t &>/dev/null
                        if [ $? -eq 0 ];then
                            echo "Nginx语法检查成功...."
                            /usr/sbin/nginx &>/dev/null && sleep 2
                            if [ $? -eq 0 ];then
                                action "服务Nginx启动成功...." /bin/true
                            else
                                action "服务Nginx启动失败...." /bin/false
                            fi
                        else
                            echo "语法检查还是失败!你自己手动修改去吧!"
                        fi
                        ;;
                    No|N|n)
                        echo "你选择了不修改配置文件!你可以手动修改!"
                        ;;
                    *)
                        echo "你输入不符合要求!请重新输入!"
                esac
            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 -t &>/dev/null
            if [ $? -eq 0 ];then
                echo "Nginx语法检查成功...."
                /usr/sbin/nginx &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "服务Nginx启动成功...." /bin/true
                else
                    action "服务Nginx启动失败...." /bin/false
                fi
            else
                Error=/tmp/nginx_error.log
                /usr/sbin/nginx -t &>$Error
                Nginx_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
                Nginx_Line=$(awk  -F '[ :]' 'NR==1{print $NF}' $Error)
                /usr/sbin/nginx -t
                read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
                case $Confirm in
                    Yes|y|Y)
                        vim +$Nginx_Line  $Nginx_File
                        /usr/sbin/nginx -t &>/dev/null
                        if [ $? -eq 0 ];then
                            echo "Nginx语法检查成功...."
                            /usr/sbin/nginx -s stop &>/dev/null && sleep 2
                            /usr/sbin/nginx &>/dev/null && sleep 2
                            if [ $? -eq 0 ];then
                                action "服务Nginx启动成功...." /bin/true
                            else
                                action "服务Nginx启动失败...." /bin/false
                            fi
                        else
                            echo "语法检查还是失败!你自己手动修改去吧!"
                        fi
                        ;;
                    No|N|n)
                        echo "你选择了不修改配置文件!你可以手动修改!"
                        ;;
                    *)
                        echo "你输入不符合要求!请重新输入!"
                esac
            fi
        else
            action "服务Nginx不在运行中...." /bin/true
            /usr/sbin/nginx -t &>/dev/null
            if [ $? -eq 0 ];then
                echo "Nginx语法检查成功...."
                /usr/sbin/nginx &>/dev/null && sleep 2
                if [ $? -eq 0 ];then
                    action "服务Nginx启动成功...." /bin/true
                else
                    action "服务Nginx启动失败...." /bin/false
                fi
            else
                Error=/tmp/nginx_error.log
                /usr/sbin/nginx -t &>$Error
                Nginx_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
                Nginx_Line=$(awk  -F '[ :]' 'NR==1{print $NF}' $Error)
                /usr/sbin/nginx -t
                read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
                case $Confirm in
                    Yes|y|Y)
                        vim +$Nginx_Line  $Nginx_File
                        /usr/sbin/nginx -t &>/dev/null
                        if [ $? -eq 0 ];then
                            echo "Nginx语法检查成功...."
                            /usr/sbin/nginx &>/dev/null && sleep 2
                            if [ $? -eq 0 ];then
                                action "服务Nginx启动成功...." /bin/true
                            else
                                action "服务Nginx启动失败...." /bin/false
                            fi
                        else
                            echo "语法检查还是失败!你自己手动修改去吧!"
                        fi
                        ;;
                    No|N|n)
                        echo "你选择了不修改配置文件!你可以手动修改!"
                        ;;
                    *)
                        echo "你输入不符合要求!请重新输入!"
                esac
            fi
        fi
        ;;
    reload)
        if [ -f $Pid_File ];then
            /usr/sbin/nginx -t &>/dev/null
            if [ $? -eq 0 ];then
                echo "Nginx语法检查成功...."
                /usr/sbin/nginx -s reload  &>/dev/null  && sleep 2
                if [ $? -eq 0 ];then
                    action "服务Nginx平滑重启成功...."  /bin/true
                else
                    action "服务Nginx平滑重启失败...."  /bin/false
                fi
            else
                Error=/tmp/nginx_error.log                                                                                            
                /usr/sbin/nginx -t &>$Error
                Nginx_File=$(awk  -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
                Nginx_Line=$(awk  -F '[ :]' 'NR==1{print $NF}' $Error)
                /usr/sbin/nginx -t
                read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
                case $Confirm in
                    Yes|y|Y)
                        vim +$Nginx_Line  $Nginx_File
                        /usr/sbin/nginx -t &>/dev/null
                        if [ $? -eq 0 ];then
                            echo "Nginx语法检查成功...."
                            /usr/sbin/nginx -s reload  &>/dev/null && sleep 2
                            if [ $? -eq 0 ];then
                                action "服务Nginx平滑重启成功...." /bin/true
                            else
                                action "服务Nginx平滑重启失败...." /bin/false
                            fi
                        else
                            echo "语法检查还是失败!你自己手动修改去吧!"
                        fi
                        ;;
                    No|N|n)
                        echo "你选择了不修改配置文件!你可以手动修改!"
                        ;;
                    *)
                        echo "你输入不符合要求!请重新输入!"
                esac
            fi
        else
            action "服务Nginx不在运行中!无法进行平滑重启操作...." /bin/false
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status|reload|restart}"
esac

#解锁
rm -f $Suo

[root@shell /service/scripts/day05]# sh case-4.sh  restart
服务Nginx停止失败....                                      [FAILED]
nginx: [emerg] unknown directive "id" in /etc/nginx/nginx.conf:7
nginx: configuration file /etc/nginx/nginx.conf test failed
Nginx配置文件语法检查失败!错误的配置文件为:/etc/nginx/nginx.conf错误的行为第7行:是否进行配置修改[Yes|No]:y
Nginx语法检查成功....
服务Nginx启动成功....                                      [  OK  ]

脚本四:函数方式

[root@shell ~]# cat /server/scripts/day05/nginx.sh
#!/bin/bash
#定义变量
Pid_File=/var/run/nginx.pid

#定义函数
ngxstart(){
    if [ -f $Pid_File ];then
        action "nginx服务正在运行中......" /bin/true
    else
        /usr/sbin/nginx -t &> /dev/null
        if [ $? -eq 0 ];then
            action "nginx语法检查成功!" /bin/true
            /usr/sbin/nginx &> /dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "nginx服务启动成功!" /bin/true
            else
                action "nginx服务启动失败!" /bin/false
                exit  
            fi  
        else
            Error_File=/tmp/nginx_error.log
            /usr/sbin/nginx -t &> $Error_File
            action "nginx语法检查失败!" /bin/false
            cat $Error_File
        fi
    fi       
}              

ngxstop(){
    if [ -f $Pid_File ];then
        /usr/sbin/nginx -t &> /dev/null
        if [ $? -eq 0 ];then
            action "nginx语法检查成功!" /bin/true
            /usr/sbin/nginx -s stop &> /dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "nginx服务停止成功!" /bin/true
            else
                action "nginx服务停止失败!" /bin/false
            fi
        else
            Error_File=/tmp/nginx_error.log
            /usr/sbin/nginx -t &> $Error_File
            action "nginx语法检查失败!" /bin/false
            cat $Error_File
        fi
    else
        action "nginx服务没有运行!" /bin/true
    fi
}

ngxstatus(){
    if [ -f $Pid_File ];then
        action "nginx服务正在运行中..." /bin/true 
    else
        action "nginx服务没有运行!" /bin/true 
    fi
}

ngxreload(){
    if [ -f $Pid_File ];then
        /usr/sbin/nginx -t &> /dev/null
        if [ $? -eq 0 ];then
            action "nginx语法检查成功!" /bin/true
            /usr/sbin/nginx -s reload &> /dev/null && sleep 2
            if [ $? -eq 0 ];then
                action "nginx服务平滑重启成功!" /bin/true
            else
                action "nginx服务平滑重启失败!" /bin/true
            fi
        else
            Error_File=/tmp/nginx_error.log
            /usr/sbin/nginx -t &> $Error_File
            action "nginx语法检查失败!" /bin/false
            cat $Error_File
        fi
    else
        action "nginx服务未运行!" /bin/false
    fi
}


#加锁
Suo=/tmp/nginx.lock
#判断是否存在锁机制
if [ -f $Suo ];then
    echo "此脚本${0}正在运行中!请稍后再执行...."
    exit
fi

#引用系统函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions

#判断执行脚本时的位置变量是否存在且唯一
if [ $# -ne 1 ];then
    echo "Usage:$0 {start|stop|status|reload|restart}"
    exit
fi

#给脚本加锁,脚本运行过程中无法被再次调用
touch $Suo


#编写case语句,执行位置变量所对应的任务
case $1 in
    start)
        ngxstart #调用函数
        ;;
    stop)
        ngxstop #调用函数
        ;;
    status)
        ngxstatus #调用函数
        ;;
    restart)
        ngxstop #调用函数
        ngxstart #调用函数 
        ;;
    reload)
        ngxreload #调用函数
        ;;
    *)
esac

#解锁
rm -f $Suo