Nginx运维脚本分享

Nginx运维脚本

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 "$@"

  • 定时任务检查功能尚未完善,后面慢慢更新。。。
### Nginx 运维常用命令总结 #### 1. 启动、停止和重启 Nginx 启动、停止或重新加载 Nginx 配置文件时,可以使用以下命令: - 启动 Nginx: ```bash nginx ``` - 停止 Nginx(快速停止): ```bash nginx -s stop ``` - 平滑停止 Nginx(等待现有连接处理完毕): ```bash nginx -s quit ``` - 重新加载配置文件: ```bash nginx -s reload ``` 验证配置文件语法是否正确: ```bash nginx -t ``` 此命令会检查配置文件是否存在语法错误[^1]。 #### 2. 查看 Nginx 进程 在 Linux 或 MacOS 环境下,可以通过以下命令查看 Nginx 的进程信息: ```bash ps -ef | grep nginx ``` 上述命令将显示所有与 Nginx 相关的进程及其详细信息,例如 PID 和用户信息[^2]。 #### 3. 编译安装 Nginx 如果需要重新编译 Nginx 或添加模块,可以按照以下步骤操作: - 下载并解压 Nginx 源码包后,执行 `./configure` 命令进行配置。例如: ```bash ./configure --prefix=/usr/local/nginx --user=nginx --with-http_ssl_module ``` - 编译完成后,仅执行 `make` 而不执行 `make install`,以避免覆盖原有配置文件[^3]。 #### 4. 日志管理 Nginx 的日志文件通常位于 `/var/log/nginx/` 目录下,包括访问日志和错误日志。可以通过以下脚本定期备份和压缩日志文件: ```bash #!/bin/bash dir=$1 name=`date -d yesterday +%Y%m%d` errorFileName=$dir/error.log nginxFileName=$dir/access.log newErrorFileName=$dir/error.$name.log newNginxFileName=$dir/access.$name.log cp $errorFileName $newErrorFileName cat /dev/null > $errorFileName cp $nginxFileName $newNginxFileName cat /dev/null > $nginxFileName tar -zcvf $newErrorFileName.tar.gz $newErrorFileName rm -rf $newErrorFileName tar -zcvf $newNginxFileName.tar.gz $newNginxFileName rm -rf $newNginxFileName find $dir/ -mtime +$2 -type f -name *.tar.gz | xargs rm -f ``` 该脚本会将前一天的日志文件复制、压缩并删除原始日志内容[^4]。 #### 5. 性能优化 为了提高 Nginx 的性能,可以调整以下参数: - 修改 worker_processes 数量为 CPU 核心数: ```nginx worker_processes auto; ``` - 设置每个 worker_process 的最大连接数: ```nginx worker_connections 1024; ``` #### 6. 常见问题排查 如果 Nginx 出现问题,可以参考以下方法排查: - 检查端口是否被占用: ```bash netstat -tuln | grep 80 ``` - 查看错误日志: ```bash tail -f /var/log/nginx/error.log ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值