FastAPIをサービス化したい

fastAPIを使った以下のプログラム(test.py)を端末の起動時に自動的に立ち上がるようにしたいです

python

#!/usr/bin/python3.8from fastapi import FastAPI import uvicorn app = FastAPI()if __name__ == "__main__": uvicorn.run(app)

作ったサービスファイルは以下

[Unit] Description=starter Daemon [Service] ExecStart=/bin/bash -c 'python3.8 /home/test.py' Restart=always Type=simple [Install] WantedBy=multi-user.target

しかし、実行してもうまくいきません。

● test.service - test Daemon Loaded: loaded (/lib/systemd/system/test.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2022-09-05 18:22:34 JST; 9s ago Process: 1936 ExecStart=/bin/bash -c python3.8 /home/test.py (code=exited, status=1/FAILURE) 9月 05 18:22:34 aisandbox-df systemd[1]: starter.service: Scheduled restart job, restart counter is at 5. 9月 05 18:22:34 aisandbox-df systemd[1]: Stopped starter Daemon. 9月 05 18:22:34 aisandbox-df systemd[1]: test.service: Start request repeated too quickly. 9月 05 18:22:34 aisandbox-df systemd[1]: test.service: Failed with result 'exit-code'. 9月 05 18:22:34 aisandbox-df systemd[1]: Failed to start starter Daemon.

サービスファイルを通さずにpython3.8 test.pyで実行すると動きます。test.pyの内容をただ10秒ごとにあ、とprintしつづけるプログラムに変えても動きます。
サーバーを立ち上げるfastAPIをサービス化するときのみ動きません。

原因と解決策が分かる方、教えてください。

コメントを投稿

0 コメント