Nginx 热升级脚本示例

#!/bin/bash

set -x
set -e

nginx_bin_file="/usr/sbin/nginx"
nginx_pid_file="/var/run/nginx.pid"
new_bin_file="/opt/nginx"

logger(){
    echo `date +%Y-%m-%d\ %H:%M:%S` $@
}

cp -a ${nginx_bin_file} ${nginx_bin_file}.old
chmod +x ${new_bin_file}
rm -f ${nginx_bin_file}
cp -a ${new_bin_file} ${nginx_bin_file}

${nginx_bin_file} -t > /dev/null 2> null
if [ $? -ne 0 ]
then
    ${nginx_bin_file} -t
    logger "nginx -t check failed, exit 1"
    exit 1
fi

pid1=`cat ${nginx_pid_file}`
pid2=`ps -ef| grep ${nginx_bin_file} | grep master | awk '{print $2}'`
if [ x"${pid1}" != x"${pid2}" ]
then
    logger "pid not match, exit 1"
    exit 1
fi

kill -USR2 `cat ${nginx_pid_file}`
sleep 60

new_pid=`cat ${nginx_pid_file}`
proc_count=`ps -ef | awk '{print $3}' | grep -w ${new_pid} | wc -l`
if [ ${proc_count} -lt 1 ]
then
    logger "proc count check err, exit 1"
    exit 1
fi

kill -WINCH `cat ${nginx_pid_file}.oldbin`
sleep 60

kill -QUIT `cat ${nginx_pid_file}.oldbin`

官方文档:https://nginx.org/en/docs/control.html#upgrade

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容