1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

systemctl.serviceの自作と反映

Posted at

概要

  • ステージング環境用にAmazonLinux 2023に移動したが、pythonのFastAPIとphp のque workerのプロセス管理のために、systemctlを利用しようと考えた

設定内容

fastapiとしていますが、名称に関しては自由に置き換えてください

sudo vim /etc/systemd/system/fastapi.service
/etc/systemd/system/fastapi.service
[Unit]
# サービスの説明
Description=FastAPI
# ネットワークが利用可能になった後にこのサービスを開始する
After=network.target

[Service]
# このサービスを実行するユーザー
User=ec2-user
# アプリケーションの作業ディレクトリ
WorkingDirectory=/home/ec2-user/python
# 実行するコマンド(仮想環境のPythonを使用してアプリケーションを起動)
ExecStart=/home/ec2-user/python/venv/bin/python /home/ec2-user/python/app.py

# プロセスが終了したら常に再起動する
Restart=always
# 再起動の間に10秒待機する
RestartSec=10

# 短時間での過剰な再起動を防ぐための制限
# 5分間(300秒)に最大5回まで再起動を試みる
StartLimitBurst=5
StartLimitIntervalSec=300

# 標準出力と標準エラー出力をジャーナルに送る
StandardOutput=journal
StandardError=journal

[Install]
# マルチユーザーモードで起動時に、このサービスを自動的に開始する
WantedBy=multi-user.target

設定の反映

# systemdにサービスファイルの変更を認識させる
sudo systemctl daemon-reload
# サービスを起動する
sudo systemctl start fastapi
# システム起動時に自動的にサービスを開始するように設定
sudo systemctl enable fastapi
# ステータスの確認
sudo systemctl status fastapi

ざつまとめ

簡単にプロセス管理ができるので、結構ありと感じた

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?