为 Flask 应用程序创建系统服务时如何解决 (code=exited,status=203/Exec)

SAI*_*RAG 2 python nginx flask uwsgi

我正在尝试使用系统服务来部署烧瓶应用uWSGI程序nginx。我看了很多教程,除了文件的更改之外,所有教程都有相同的过程.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
Run Code Online (Sandbox Code Playgroud)

我得到的服务文件的状态如下:

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.
Run Code Online (Sandbox Code Playgroud)

我尝试从其他来源更改服务文件,并尝试更改目录权限,但仍然出现相同的错误,并且无法使用该服务启动应用程序。虽然我可以使用以下命令使用 uwsgi 手动运行该应用程序 -uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi

小智 5

您的ExecStart需求已更新。您必须提供文件的位置和解释器。由于您使用的是 python,因此您的代码应该是这样的:

[Unit]
Description=Flask web server

[Service]
User=schirag
PermissionsStartOnly=true
ExecStart=/usr/bin/python3 /home/schirag/server.py
TimeoutSec=600
Restart=on-failure
RuntimeDirectoryMode=755

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

使用命令type python3获取python的位置。