如何解决为Flask应用创建system服务时报(code=exited,status=203/Exec)错误
问题描述
我正尝试通过systemd服务使用uWSGI和Nginx部署Flask应用,查阅的众多教程操作流程一致,仅.service文件配置存在差异。
我的服务文件配置如下:
[Unit] Description=Flask web server [Install] WantedBy=multi-user.target [Service] User=schirag PermissionsStartOnly=true ExecStart=/home/schirag/server.py TimeoutSec=600 Restart=on-failure RuntimeDirectoryMode=755
查看服务状态返回如下报错:
flask.service - Flask web server Loaded: loaded (/etc/systemd/system/flask.service; disabled; vendor preset: disable> Active: failed (Result: exit-code) since Wed 2021-08-11 11:20:25 IST; 6s ago Process: 13131 ExecStart=/home/schirag/server.py (code=exited, status=203/EXEC) Main PID: 13131 (code=exited, status=203/EXEC) Aug 11 11:20:24 localhost.localdomain systemd[1]: flask.service: Main process exited, code=exited, status=203/EXEC Aug 11 11:20:24 localhost.localdomain systemd[1]: flask.service: Failed with result 'exit-code'. Aug 11 11:20:25 localhost.localdomain systemd[1]: flask.service: Service RestartSec=100ms expired, scheduling restart. Aug 11 11:20:25 localhost.localdomain systemd[1]: flask.service: Scheduled restart job, restart counter is at 5. Aug 11 11:20:25 localhost.localdomain systemd[1]: Stopped Flask web server. Aug 11 11:20:25 localhost.localdomain systemd[1]: flask.service: Start request repeated too quickly. Aug 11 11:20:25 localhost.localdomain systemd[1]: flask.service: Failed with result 'exit-code'. Aug 11 11:20:25 localhost.localdomain systemd[1]: Failed to start Flask web server.
我尝试过参考其他来源修改服务文件,也调整过目录权限,但依旧出现相同错误,无法通过服务启动应用。不过我可以通过uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi命令手动正常运行uWSGI启动应用,请问该如何排查解决此问题?
解决方案
status=203/EXEC报错的核心原因是systemd无法正常执行你在ExecStart中指定的命令,按以下步骤排查修正即可:
- 修正
.service文件配置
你手动验证可用的启动命令是uWSGI调用,不需要直接执行server.py,先补全服务配置的缺失项,最终[Service]段参考配置如下:
如果你坚持要直接执行[Service] User=schirag # 填写你的项目根目录(即wsgi文件所在的目录) WorkingDirectory=/home/schirag/你的项目目录 # 先执行which uwsgi获取你环境中uwsgi的绝对路径,替换下面的路径 ExecStart=/usr/bin/uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi PermissionsStartOnly=true TimeoutSec=600 Restart=on-failure RuntimeDirectoryMode=755server.py启动,需要确认两个点:server.py首行添加了正确的Python解释器shebang(如#!/usr/bin/env python3),且已经给文件加了执行权限chmod +x /home/schirag/server.py。 - 重载配置验证
修改完.service文件后,必须先执行systemctl daemon-reload重载systemd的配置,再执行systemctl start flask启动服务,即可正常运行。
内容的提问来源于stack exchange,提问作者SAI SANTOSH CHIRAG
相关产品推荐
相关产品推荐

