1、介绍
nginx运维脚本,包含启动,停止,重启,重新加载配置,平滑升级,服务状态检查
使用方法:
- 在脚本同级目录下新建nginxPath.txt文件,输入nginx运行目录,如:/home/jiang/resty/bin/nginx
- 执行sh control.sh start启动nginx
2、源代码
脚本名control.sh
#!/bin/bash
ACTION=$1
curr_dir=$(cd "$(dirname "$0")" || exit; pwd) # 当前脚本所在目录
nginx_path=$(cat "${curr_dir}"/nginxPath.txt) # nginx运行目录
pid_file="${nginx_path}/logs/nginx.pid"
# 服务状态检查
function check()
{
local ok=0
# 1、master进程数检查
master_count=$(ps -ef | grep -v grep | grep -c "master process" )
if (( master_count > 1 )); then
echo "[check] Error, master count is more than one!"
ok=1
elif (( master_count > 1 )); then
echo "[check] Error, master count is more than one!"
ok=1
else
echo "[check] OK, The process is running normally!"
fi
# 2、pid文件检查
if [ -f "$pid_file" ]; then
pid=$(cat "${pid_file}")
master_pid=$(ps -ef | grep -v grep | grep "master process" | awk '{print $2}')
if [ "y${pid}" == "y${master_pid}" ]; then
echo "[check] OK, Pid file no problem!"
else
echo "[check] Error, Pid file not correct!"
ok=1
fi
else
echo "[check] Error, Pid file does not exist!"
ok=1
fi
# 3、定时任务检查
echo "[check] OK, Timer task ok!" # 暂未添加定时任务
return "${ok}"
}
# 启动服务
function start()
{
master_count=$(ps -ef | grep -v grep | grep -c "master process" )
if (( master_count >= 1 )); then
echo "[start] OK, nginx is alredy running, nothing to do!"
return 1
fi
"${nginx_path}"/sbin/nginx -t # 测试配置文件
if [ $? != 0 ]; then # 配置文件语法错误
echo "[start] Error, Nginx conf file test error!!!!!!"
return 1
fi
"${nginx_path}"/sbin/nginx # 启动nginx
sleep 0.5
master_count=$(ps -ef | grep -v grep | grep -c "master process" )
if (( master_count != 1 )); then
echo "[start] Error, Conf ok but master process start failed!!!!!!"
return 1
fi
ps -ef | grep nginx
echo "[start] OK"
return 0
}
# 停止服务
function stop()
{
nginx_count=$(ps -ef | grep -v grep | grep -c "nginx:" )
if (( nginx_count == 0 )); then # 已经停止运行了,退出
echo "[stop] OK, nginx has stopped, nothing to do!"
return 1
fi
"${nginx_path}"/sbin/nginx -s stop
sleep 0.5
nginx_count=$(ps -ef | grep -v grep | grep -c "nginx:" )
if (( nginx_count != 0 )); then
echo "[stop] Error, stop failed!" # stop失败
return 1
fi
echo "[stop] OK"
return 0
}
# 平滑升级
function update()
{
if [ ! -f "$pid_file" ]; then
echo "[update] Error, Pid file does not exist!!!!!!"
return 1
fi
master_count=$(ps -ef | grep -v grep | grep -c "master process" )
if (( master_count != 1 )); then
echo "[update] Error, Master count does not equal 1 !!!!!!"
return 1
fi
pid=$(cat "${pid_file}")
kill -USR2 "${pid}" # 自动拉起一组新进程(master和worker)
sleep 0.5 # 此处有待优化,应该循环等待master进程数为2,然后往下执行
kill -WINCH "${pid}" # 老worker进程退出
kill -QUIT "${pid}" # master进程退出
echo "[update] OK"
check
return $?
}
# 脚本使用方法
function usage()
{
echo ""
echo "=============control 脚本使用方法============="
echo " 1、启动服务 sh control.sh start"
echo " 2、停止服务 sh control.sh stop"
echo " 3、重启服务 sh control.sh restart"
echo " 4、重新加载配置文件 sh control.sh reload"
echo " 5、服务平滑升级 sh control.sh update"
echo " 6、服务状态检测 sh control.sh check"
echo " 7、脚本使用方法 sh control.sh usage"
echo "============================================="
echo ""
}
# 入口函数
function main()
{
case "${ACTION}" in
start)
start # 启动
;;
stop)
stop # 停止
;;
restart)
stop # 重启
start
;;
reload)
start # 重新加载配置文件
stop
;;
update)
update # 平滑升级
;;
check)
check # 服务状态检查
;;
*)
usage # 参数错误
;;
esac
exit $?
}
main "$@"
- 定时任务检查功能尚未完善,后面慢慢更新。。。