uWSGI 不会在 Ubuntu 16.04 下以 systemd 启动

mat*_*hew 7 ubuntu uwsgi systemd ubuntu-16.04

我正在将我们的临时和生产服务器从 Ubuntu 12.04 迁移到 16.04,过程中有些痛苦。我正在测试暂存时的迁移,除了让 uWSGI 在 systemd 下启动(以前在 Upstart 下运行良好)外,它大部分都在工作。这没有问题:

uwsgi --ini /etc/uwsgi/my_wsgi.ini
Run Code Online (Sandbox Code Playgroud)

但是运行以下命令不起作用(uWSGI 不会启动,但不会产生错误):

sudo systemctl start uwsgi
Run Code Online (Sandbox Code Playgroud)

我在 /etc/systemd/system/uwsgi.service 中创建了以下服务:

[Unit]
Description=uWSGI Service

[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/my_wsgi.ini
Restart=always
RestartSec=5
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

和 my_wsgi.ini 有以下内容:

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/project/hidden
# Django's wsgi file
module          = wsgi
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 8
# the socket (use the full path to be safe)
socket          = /path/to/socket/hidden
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
# Mercilessly kill this worker if it takes longer than this time to reload
reload-mercy    = 8
# Reload workers after the specified amount of managed requests (avoid memory leaks)
max-requests    = 5000
# Every request that will take longer than the seconds specified in the harakiri timeout will be dropped and the corresponding worker is thereafter recycled.
harakiri        = 90
# Log everything and make daemon
daemonize       = /var/log/uwsgi/my.log
# use custom settings file
env             = DJANGO_SETTINGS_MODULE=settings.staging
# set pid file for starting/stopping server
pidfile         = /path/to/pid/hidden.pid
# See https://docs.newrelic.com/docs/python/python-agent-and-uwsgi
enable-threads  = true
single-interpreter = true
Run Code Online (Sandbox Code Playgroud)

运行不systemd-analyze verify uwsgi.service返回任何内容并sudo journalctl返回以下内容:

Starting uWSGI Service...
[uWSGI] getting INI configuration from /etc/uwsgi/my_wsgi.ini
Started uWSGI Service.
uwsgi.service: Service hold-off time over, scheduling restart.
Stopped uWSGI Service.
... (repeats a few times) ....
Run Code Online (Sandbox Code Playgroud)

我怀疑这uwsgi.service: Service hold-off time over, scheduling restart.可能指向了这个问题,但我一直无法弄清楚。

Mic*_*ton 7

uwsgi文件明确规定:

注意:除非您知道自己在做什么,否则不要守护皇帝(或主人)!!!

不幸的是,你在不知道自己在做什么的情况下将皇帝恶魔化了。从你的问题:

# Log everything and make daemon
daemonize       = /var/log/uwsgi/my.log
Run Code Online (Sandbox Code Playgroud)

问题是 uwsgi 是 systemd 感知的,systemd 单元告诉 systemd 当 uwsgi 准备好开始处理请求时,uwsgi 会通知 systemd。默认情况下,uwsgi在 systemd 系统上以这种模式启动,除非你告诉它守护进程。在这种情况下,uwsgi不会与 systemd 对话。这就是为什么 systemd 等了一会儿,最后放弃等待 uwsgi 告诉 systemd 已经准备好了。

与其让 uwsgi daemonize 并直接记录到文件,文档建议您应该让它记录到默认的标准错误,并让 systemd 获取日志并记录它们。如果您最终将日志发送到其他地方,例如 ELK 堆栈,您将需要这样做。例如:

[Service]
StandardError=syslog
Run Code Online (Sandbox Code Playgroud)

或者你可以直接从 uwsgi 登录而不需要守护进程。

logto = /var/log/uwsgi/my.log
Run Code Online (Sandbox Code Playgroud)