#基本概述
语法:
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 ]