配置uwsgi为系统服务遇到State ‘stop-sigterm‘ timed out解决方法

[root@localhost learning_log]# systemctl start uwsgi
启动服务
[root@localhost learning_log]# systemctl status uwsgi
uwsgi.service - The uWSGI server
   Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2025-06-25 12:10:34 CST; 6s ago
。。。
一切正常

[root@localhost learning_log]# systemctl stop uwsgi
停止服务花了五分钟时间左右

[root@localhost learning_log]# systemctl status uwsgi
uwsgi.service - The uWSGI server
   Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; disabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Wed 2025-06-25 12:13:10 CST; 7s ago
     Docs: https://uwsgi-docs.readthedocs.io/en/latest/
  Process: 104929 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /home/zhang/run/project-master.pid) (code=exited, status=0/SUCCESS)
  Process: 104905 ExecStart=/home/zhang/learning_log/ll_env/bin/uwsgi --ini ${conffile} (code=exited, status=0/SUCCESS)
 Main PID: 104907 (code=killed, signal=KILL)

Jun 25 12:10:33 localhost.localdomain uwsgi[104905]: [uWSGI] getting INI configuration from /home/zhang/learning_log/uwsgi.ini
Jun 25 12:10:33 localhost.localdomain systemd[1]: uwsgi.service: Can't open PID file /home/zhang/run/project-master.pid (yet?) after start: No such file or directory
Jun 25 12:10:34 localhost.localdomain systemd[1]: Started The uWSGI server.
Jun 25 12:11:40 localhost.localdomain systemd[1]: Stopping The uWSGI server...
Jun 25 12:13:10 localhost.localdomain systemd[1]: uwsgi.service: State 'stop-sigterm' timed out. Killing.
Jun 25 12:13:10 localhost.localdomain systemd[1]: uwsgi.service: Killing process 104907 (uwsgi) with signal SIGKILL.
Jun 25 12:13:10 localhost.localdomain systemd[1]: uwsgi.service: Killing process 104932 (uwsgi) with signal SIGKILL.
Jun 25 12:13:10 localhost.localdomain systemd[1]: uwsgi.service: Main process exited, code=killed, status=9/KILL
Jun 25 12:13:10 localhost.localdomain systemd[1]: uwsgi.service: Failed with result 'timeout'.

Jun 25 12:13:10 localhost.localdomain systemd[1]: Stopped The uWSGI server.
活动状态不正常:failed。粗体字显示停止服务异常,红色字体信息显示用sigterm信号stop服务超时,改用Kill signal。虽然最终Stopped The uWSGI server,但是每次停止服务要花五分钟时间总嫌太费时


配置文件/usr/lib/systemd/system/uwsgi.service内容如下,文件模版是从nginx配置里复制过来的
[Unit]
Description=The uWSGI server
Documentation=https://uwsgi-docs.readthedocs.io/en/latest/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
#Type=simple
PIDFile=/home/zhang/run/project-master.pid
Environment="conffile=/home/zhang/learning_log/uwsgi.ini"
#Environment="conffile=/etc/nginx/nginx.conf"
EnvironmentFile=-/etc/sysconfig/uwsgi
ExecStart=/home/zhang/learning_log/ll_env/bin/uwsgi --ini ${conffile}
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /home/zhang/run/project-master.pid)"
#ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /home/zhang/run/project-master.pid)"
ExecStop=/home/zhang/learning_log/ll_env/bin/uwsgi --stop /home/zhang/run/project-master.pid

#KillMode=mixed

[Install]
WantedBy=multi-user.target
停止服务的命令改用uwsgi --stop /home/zhang/run/project-master.pid就OK了。如果非要用kill命令停止服务,要传递SIGINT信号,而不是TERM信号,即:

ExecStop=/bin/sh -c "/bin/kill -s INT $(/bin/cat /home/zhang/run/project-master.pid)"

效果相当于uwsgi在foreground运行时按下Ctrl+c

你可能感兴趣的:(linux系统服务,linux,uwsgi,systemctl)