我将按照本文档为我的gunicorn服务器设置Systemd套接字和服务。
- Systemd启动gunicorn为
www-data
- gunicorn自己分叉(默认行为)
- 服务器启动
subprocess.Popen()
子进程 - 子进程没有错误地完成,但是父进程一直从
p.poll()
获得None而不是退出代码 - 子进程终止
进程层次结构如下:
$ ps eauxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
...
www-data 14170 0.0 0.2 65772 20452 ? Ss 10:57 0:00 /usr/bin/python /usr/bin/gunicorn digits.webapp:app --pid /run/digits/pid --config /usr/lib/python2.7/dist-packages/digits/gunicorn_config.py
www-data 14176 0.8 3.4 39592776 283124 ? Sl 10:57 0:05 _ /usr/bin/python /usr/bin/gunicorn digits.webapp:app --pid /run/digits/pid --config /usr/lib/python2.7/dist-packages/digits/gunicorn_config.py
www-data 14346 5.0 0.0 0 0 ? Z 11:07 0:01 _ [python] <defunct>
:当我以root
而不是www-data
的身份运行服务时,一切都如预期的那样工作。子进程结束,父进程立即获得子进程的返回码。
/lib/systemd/system/digits.service
[Unit]
Description=DIGITS daemon
Requires=digits.socket
After=local-fs.target network.target
[Service]
PIDFile=/run/digits/pid
User=www-data
Group=www-data
Environment="DIGITS_JOBS_DIR=/var/lib/digits/jobs"
Environment="DIGITS_LOGFILE_FILENAME=/var/log/digits/digits.log"
ExecStart=/usr/bin/gunicorn digits.webapp:app
--pid /run/digits/pid
--config /usr/lib/python2.7/dist-packages/digits/gunicorn_config.py
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
/lib/systemd/system/digits.socket
[Unit]
Description=DIGITS socket
[Socket]
ListenStream=/run/digits/socket
ListenStream=0.0.0.0:34448
[Install]
WantedBy=sockets.target
/usr/lib/tmpfiles.d/digits.conf
d /run/digits 0755 www-data www-data -
我今天在CentOS-7上遇到了同样的问题。我最终克服了这个问题,忽略了本文档中的指令——它指示使用/run/
层次结构来创建套接字——而使用/tmp/
。这工作。
请注意,我的PID文件仍然放在/run/
下面(没有问题)。
总之,与其把你的套接字放在/run/...
下面,不如试着把它放在/tmp/...
下面。它在CentOS-7与systemd上为我工作。