【问题标题】:Unable to run flask application as a service with systemd on Centos 7 server无法在 Centos 7 服务器上使用 systemd 将烧瓶应用程序作为服务运行
【发布时间】:2020-06-02 19:58:03
【问题描述】:

我可以像这样在 virtualenv /web_services/flask_api/flask_api 中运行烧瓶应用程序:

gunicorn --workers=4 --bind=localhost:8000 --log-level=error app:app

但是,当我尝试将其作为服务运行时,它不起作用。这是我创建 .service 配置文件的帖子:

https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd

这是我的web_service.service 文件的样子:

[Unit]
Description=Sample web service
After=network.target 

[Service]
User=aaa.bbb
WorkingDirectory=/web_services/flask_api/flask_api
ExecStart=/web_services/flask_api/flask_api/bin/gunicorn --workers=4 --bind=localhost:8000 --log-level=error web_service:app
Restart=always 

[Install]
WantedBy=multi-user.target

保存此文件后,我做了:

$ sudo systemctl daemon-reload
$ sudo systemctl start web_service
$ sudo systemctl enable web_service

systemctl | grep running 没有显示这个 api。

在检查此服务的状态时,我得到了这个:

sudo systemctl status web_service
● web_service.service - Sample web service  
 Loaded: loaded (/etc/systemd/system/web_service.service; enabled; vendor preset: disabled)   
 Active: failed (Result: start-limit) since Mon 2020-06-01 16:15:15 EDT; 13s ago 
 Process: 1016 ExecStart=/web_services/flask_api/flask_api/bin/gunicorn --workers=4 --bind=localhost:8000 --log-level=error web_service:app (code=exited, status=1/FAILURE)
 Main PID: 1016 (code=exited, status=1/FAILURE)

不知道我在这里错过了什么或做错了什么。任何帮助将不胜感激..

【问题讨论】:

  • 1) 您可以使用journalctl -e -u web_service 查看其他日志,错误消息可能会对您有所帮助。 2)据我所知,我在使用 virtualenv 时遇到了最大的麻烦,但我也听说对于“普通”虚拟环境,它应该像文章中那样工作,只需从虚拟环境中调用 gunicorn。我有使用environment=PATH=.../env/bin:%(ENV_PATH)s"command=bash -c ".../env/bin/activate && gunicorn ... 切换到虚拟环境的工作服务(systemd 和supervisord)。但是感觉很脏,所以不值得回答:)
  • 非常感谢,通过查看日志我可以找出问题所在。它得到了修复

标签: python flask centos7 gunicorn systemd


【解决方案1】:

通过改变解决了:

ExecStart=/web_services/flask_api/flask_api/bin/gunicorn --workers=4 --bind=localhost:8000 --log-level=error web_service:app

ExecStart=/web_services/flask_api/flask_api/bin/gunicorn --workers=4 --bind=localhost:8000 --log-level=error app:app

【讨论】: