我有一个用 Python 中的 FastAPI 框架编写的 Web 应用程序,将由 Nginx 提供服务。我想将应用程序添加为系统守护进程,以便它可以自动重新启动。
我的问题:手动运行它并通过 nginx 公开访问它,但是当我启动守护进程时,它只是告诉
“失败,结果为‘退出代码’”
我的Python应用程序的源代码存储在:
/home/myuser/webapp
CD 进入此目录我可以启动它:
/usr/local/bin/uvicorn main:app --host 127.0.0.1 --port 8000
所以我创建了一个服务文件:
/etc/systemd/system/fastapi.service
:
[Unit]
Description=FastAPI application
After=network.target
[Service]
User=myuser
Group=myuser
WorkingDirectory=/home/myuser/webapp
ExecStart=/usr/local/bin/uvicorn main:app --host 127.0.0.1 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
但是启动服务好像没有启动。
sudo journalctl -xeu fastapi.service:
Aug 15 19:05:53 Ubuntu-2204-jammy-amd64-base systemd[1]: fastapi.service: Start request repeated too quickly.
Aug 15 19:05:53 Ubuntu-2204-jammy-amd64-base systemd[1]: fastapi.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit fastapi.service has entered the 'failed' state with result 'exit-code'.
Aug 15 19:05:53 Ubuntu-2204-jammy-amd64-base systemd[1]: Failed to start FastAPI application.
░░ Subject: A start job for unit fastapi.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit fastapi.service has finished with a failure.
░░
░░ The job identifier is 41815 and the job result is failed.
我在这里缺少什么?
这通常是环境问题,尝试创建包含:
的fastapi.shexec > /tmp/fastapi.log 2>&1
env
echo "Starting FastAPI..."
/usr/local/bin/uvicorn main:app --host 127.0.0.1 --port 8000
然后
ExecStart=bash ./fastapi.sh
通过 /tmp/fastapi.log,您应该能够确定错误原因。