【问题标题】:Systemd + non-root Gunicorn service = defunct subprocessSystemd + 非根 Gunicorn 服务 = 已失效的子进程
【发布时间】:2017-02-05 12:03:36
【问题描述】:

我正在关注 this document 为我的 gunicorn 服务器设置 Systemd 套接字和服务。

  • Systemd 以 www-data 启动 gunicorn
  • gunicorn forks 自身(默认行为)
  • 服务器用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 -

【问题讨论】:

  • 我现在放弃并停止使用 gunicorn。不过总有一天我需要解决这个问题,所以如果有人有任何想法,请告诉我!
  • 我今天整天都有同样的问题。我按照这个文件(类似于你指出的那个)到了这封信:docs.gunicorn.org/en/stable/deploy.html (._.) Sigh。

标签: python subprocess gunicorn systemd


【解决方案1】:

我今天在 CentOS-7 上遇到了同样的问题。我最终通过忽略this document 中的指令克服了它——这表明使用/run/ 层次结构来创建套接字——我改为使用/tmp/。那行得通。

请注意,我的 PID 文件仍位于 /run/ 下方(没有问题)。

总而言之,不要将您的套接字放在/run/... 下方的某个位置,而是尝试将它放在/tmp/... 下方的某个位置。它在 CentOS-7systemd 上为我工作。

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 2020-03-25
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2012-10-15
    相关资源
    最近更新 更多