You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用配置文件启动Gunicorn后如何重启服务?

如何重启未注册systemd的Gunicorn服务

你当前是通过直接执行命令启动的Gunicorn,没有将其注册为systemd服务,所以systemctl找不到对应的单元。以下两种方法可以实现重启:

临时快速重启(利用配置里的PID文件)

你的Gunicorn配置已经指定了pidfile = "/var/run/gunicorn/dev.pid",可以直接通过这个文件操作:

  • 平滑重启(不中断现有请求):
    sudo kill -HUP $(cat /var/run/gunicorn/dev.pid)
    
  • 强制重启(先停止再启动):
    1. 终止运行的Gunicorn进程:
      sudo kill $(cat /var/run/gunicorn/dev.pid)
      
    2. 用原启动命令重新拉起服务:
      gunicorn -c config/gunicorn/dev.py
      

长期方案:配置systemd服务(推荐)

配置后就能用systemctl直接管理启动、重启、停止,步骤如下:

  1. 创建systemd服务文件:
    sudo nano /etc/systemd/system/gunicorn.service
    
  2. 粘贴以下内容(注意替换你的用户名和/path/to/your/django/project为实际信息):
    [Unit]
    Description=gunicorn daemon for Django app
    After=network.target
    
    [Service]
    User=你的用户名
    Group=www-data
    WorkingDirectory=/path/to/your/django/project
    ExecStart=/usr/bin/gunicorn -c config/gunicorn/dev.py someapp.wsgi:application
    ExecReload=/bin/kill -HUP $MAINPID
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
  3. 重新加载systemd配置:
    sudo systemctl daemon-reload
    
  4. 启动服务并设置开机自启:
    sudo systemctl start gunicorn && sudo systemctl enable gunicorn
    
  5. 之后就可以用systemctl正常重启了:
    sudo systemctl restart gunicorn
    

内容的提问来源于stack exchange,提问作者Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 09:15:58