侧边栏壁纸
  • 累计撰写 119 篇文章
  • 累计创建 25 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

记一次Nginx无法停止

梁来福
2024-06-05 / 0 评论 / 0 点赞 / 5 阅读 / 2176 字
温馨提示:
本文最后更新于 2024-06-05,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

背景

跨地区迁移了所有的服务器和数据库,其中某一台机器上的Nginx服务无法重启和停止。杀掉进程,进程守护又会启动新的子进程。查看进程显示如下

[root@docker ~]# ps -ef | grep nginx
root      7180  2240  0 23:37 ?        00:00:00 nginx: master process nginx -g daemon off;
chrony    7181  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7182  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7183  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7184  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7185  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7186  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7187  7180  0 23:37 ?        00:00:00 nginx: worker process
chrony    7188  7180  0 23:37 ?        00:00:00 nginx: worker process
root      7201  3840  0 23:37 pts/0    00:00:00 grep --color=auto nginx

过程

# 使用指令停止Nginx
nginx -s stop

# 平滑关闭Nginx
nginx -s quit

# 使用pkill命令终止所有Nginx进程
pkill nginx

# 强制终止
kill -9 7181

# 脚本终止
#!/bin/bash

# 查找所有Nginx进程的PID
nginx_pids=$(pgrep nginx)

if [ -z "$nginx_pids" ]; then
    echo "No nginx process found."
else
    echo "Killing the following nginx processes: $nginx_pids"
    # 强制终止所有Nginx进程
    kill -9 $nginx_pids
fi

# 强制终止进程显示:
[root@docker ]# nginx -s stop
nginx: [alert] kill(30338, 15) failed (3: No such process

思考

Nginx进程已经终止

在发送停止Nginx信号之前,该Nginx的进程可能已经终止或者崩溃了。

PID文件不准确

Nginx 依赖其 PID 文件(通常位于 /usr/local/nginx/logs/nginx.pid 或 /var/run/nginx.pid)来记录主进程的 PID。如果 PID 文件中的 PID 不准确或不存在,Nginx 将无法找到正确的进程,所以失败。

解决

# 手动将daemon off的进程ID 7181写入PID文件内
echo "7181" > /usr/local/nginx/logs/nginx.pid

# 停止Nginx
nginx - s stop

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
博主关闭了所有页面的评论