使用配置文件启动Gunicorn后如何重启服务?
如何重启未注册systemd的Gunicorn服务
你当前是通过直接执行命令启动的Gunicorn,没有将其注册为systemd服务,所以systemctl找不到对应的单元。以下两种方法可以实现重启:
临时快速重启(利用配置里的PID文件)
你的Gunicorn配置已经指定了pidfile = "/var/run/gunicorn/dev.pid",可以直接通过这个文件操作:
- 平滑重启(不中断现有请求):
sudo kill -HUP $(cat /var/run/gunicorn/dev.pid) - 强制重启(先停止再启动):
- 终止运行的Gunicorn进程:
sudo kill $(cat /var/run/gunicorn/dev.pid) - 用原启动命令重新拉起服务:
gunicorn -c config/gunicorn/dev.py
- 终止运行的Gunicorn进程:
长期方案:配置systemd服务(推荐)
配置后就能用systemctl直接管理启动、重启、停止,步骤如下:
- 创建systemd服务文件:
sudo nano /etc/systemd/system/gunicorn.service - 粘贴以下内容(注意替换
你的用户名和/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 - 重新加载systemd配置:
sudo systemctl daemon-reload - 启动服务并设置开机自启:
sudo systemctl start gunicorn && sudo systemctl enable gunicorn - 之后就可以用
systemctl正常重启了:sudo systemctl restart gunicorn
内容的提问来源于stack exchange,提问作者Smith
相关产品推荐
相关产品推荐

