脚本一:
[root@shell /service/scripts/day05]
#!/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 $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]
服务Nginx停止成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx不在运行中.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx不在运行中.... [ OK ]
服务Nginx启动成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx停止成功.... [ OK ]
服务Nginx启动成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx平滑重启成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx停止成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx不在运行中!无法进行平滑重启操作.... [FAILED]
脚本二:加锁机制
[root@shell /service/scripts/day05]
#!/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 $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]
此脚本case-3.sh正在运行中!请稍后再执行....
脚本三:检查语法交互式修改
[root@shell /service/scripts/day05]
#!/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 $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]
服务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 ~]
#!/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 $1 in
start)
ngxstart
;;
stop)
ngxstop
;;
status)
ngxstatus
;;
restart)
ngxstop
ngxstart
;;
reload)
ngxreload
;;
*)
esac
rm -f $Suo