gpt4 book ai didi

python - 如何将 Flask/gunicorn 应用程序作为 systemd 服务运行

转载 作者:太空宇宙 更新时间:2023-11-03 14:11:19 24 4
gpt4 key购买 nike

我可以在gunicorn中成功启动 flask 应用程序,如下所示:

$ gunicorn registry --daemon --bind x.x.x.x:8000 --workers xxx --pid xxx.api.registry.pid --error-logfile xxx.api.registry.error.log --access-logfile xxx.api.registry.access.log

现在 systemd 相关配置如下所示:

  • /etc/systemd/system/api-registry.service

    [Unit]
    Description=api-registry
    After=network.target

    [Service]
    User=app
    WorkingDirectory=xxx/code/api/registry
    EnvironmentFile=-/etc/sysconfig/api-registry
    ExecStart=/usr/bin/gunicorn --daemon $OPTIONS registry
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s TERM $MAINPID
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target

并且设置是从以下位置配置的:

  • /etc/sysconfig/api-registry

OPTIONS="--bind x.x.x.x:8000 --workers 8 --pid xxx.api.registry.pid --错误日志文件 xxx.api.registry.error.log --访问日志文件 xxx。 api.registry.access.log"

将 systemd LogLevel 设置为 debug 会生成以下日志:

Jan 27 15:26:02 my-server systemd[31817]: Executing: /usr/bin/gunicorn --daemon --bind 172.31.141.233:8000 --workers 8 --pid /home/app/ip-spotlight/mon/pid/ip-spotlight.api.registry.pid --error-logfile /home/app/ip-spotlight/log/ip-spotlight.api.registry.error.log --access-logfile /home/app/ip-spotlight/log/ip-spotlight.api.registry.access.log registry
Jan 27 15:26:03 my-server systemd[1]: Received SIGCHLD from PID 31817 (gunicorn).
Jan 27 15:26:03 my-server systemd[1]: Child 31817 (gunicorn) died (code=exited, status=0/SUCCESS)
Jan 27 15:26:03 my-server systemd[1]: Child 31817 belongs to api-registry.service
Jan 27 15:26:03 my-server systemd[1]: api-registry.service: main process exited, code=exited, status=0/SUCCESS
Jan 27 15:26:03 my-server systemd[1]: About to execute: /bin/kill -s TERM $MAINPID
Jan 27 15:26:03 my-server systemd[1]: Forked /bin/kill as 31820
Jan 27 15:26:03 my-server systemd[1]: api-registry.service changed running -> stop
Jan 27 15:26:03 my-server systemd[1]: Child 31818 (gunicorn) died (code=exited, status=0/SUCCESS)
Jan 27 15:26:03 my-server systemd[1]: Received SIGCHLD from PID 31818 (n/a).
Jan 27 15:26:03 my-server systemd[31820]: Executing: /bin/kill -s TERM
Jan 27 15:26:03 my-server kill[31820]: Usage:
Jan 27 15:26:03 my-server kill[31820]: kill [options] <pid|name> [...]
Jan 27 15:26:03 my-server kill[31820]: Options:
Jan 27 15:26:03 my-server kill[31820]: -a, --all do not restrict the name-to-pid conversion to processes
Jan 27 15:26:03 my-server kill[31820]: with the same uid as the present process
Jan 27 15:26:03 my-server kill[31820]: -s, --signal <sig> send specified signal
Jan 27 15:26:03 my-server kill[31820]: -q, --queue <sig> use sigqueue(2) rather than kill(2)
Jan 27 15:26:03 my-server kill[31820]: -p, --pid print pids without signaling them
Jan 27 15:26:03 my-server kill[31820]: -l, --list [=<signal>] list signal names, or convert one to a name
Jan 27 15:26:03 my-server kill[31820]: -L, --table list signal names and numbers
Jan 27 15:26:03 my-server kill[31820]: -h, --help display this help and exit
Jan 27 15:26:03 my-server kill[31820]: -V, --version output version information and exit
Jan 27 15:26:03 my-server kill[31820]: For more details see kill(1).
Jan 27 15:26:03 my-server systemd[1]: Received SIGCHLD from PID 31820 (kill).
Jan 27 15:26:03 my-server systemd[1]: Child 31820 (kill) died (code=exited, status=1/FAILURE)
Jan 27 15:26:03 my-server systemd[1]: Child 31820 belongs to api-registry.service
Jan 27 15:26:03 my-server systemd[1]: api-registry.service: control process exited, code=exited status=1
Jan 27 15:26:03 my-server systemd[1]: api-registry.service got final SIGCHLD for state stop
Jan 27 15:26:03 my-server systemd[1]: api-registry.service changed stop -> stop-sigterm
Jan 27 15:26:03 my-server systemd[1]: Got cgroup empty notification for: /system.slice/api-registry.service/control
Jan 27 15:26:03 my-server systemd[1]: Sent message type=signal sender=n/a destination=n/a object=/org/freedesktop/systemd1/agent interface=org.freedesktop.systemd1.Agent member=Released cookie=5775 reply_cookie=0 error=n/a
Jan 27 15:26:03 my-server systemd[1]: Received SIGCHLD from PID 31819 (gunicorn).
Jan 27 15:26:03 my-server systemd[1]: Child 31819 (gunicorn) died (code=killed, status=15/TERM)
Jan 27 15:26:03 my-server systemd[1]: Child 31819 belongs to api-registry.service
Jan 27 15:26:03 my-server systemd[1]: api-registry.service: cgroup is empty
Jan 27 15:26:03 my-server systemd[1]: api-registry.service changed stop-sigterm -> failed
Jan 27 15:26:03 my-server systemd[1]: Unit api-registry.service entered failed state.
Jan 27 15:26:03 my-server systemd[1]: api-registry.service failed.

请注意,没有 virtualenv 设置。本质上它应该像 /usr/bin/python3.4/usr/bin/gunicorn

你能告诉我我做错了什么吗?

最佳答案

事实证明--daemongunicorn选项和systemd不能一起工作

关于python - 如何将 Flask/gunicorn 应用程序作为 systemd 服务运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48476810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com