如何运行flask / gunicorn应用程序作为systemd服务

时间:2018-01-27 14:32:48

标签: python python-3.x service gunicorn systemd

我可以像这样在gunicorn中成功启动烧瓶应用程序:

$ 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-注册表

OPTIONS="--bind x.x.x.x:8000 --workers 8 --pid xxx.api.registry.pid --error-logfile xxx.api.registry.error.log --access-logfile 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

你能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

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