1. 概要
「Ubuntu 26.04」上に。
/usr/bin/python3.11 /path/to/main.py
で、動作させているプログラムがありまして。
これをデーモン化する方法について、メモします。
基本的に、「Ubuntu 22.04」でやったことと同じようにすれば、なんとかなるのではないかと思っています。
2. サービスの記述
sudo vi /etc/systemd/system/mypython.service
下記を記述します。
[Unit]
Description=My Python Application
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/path/to
ExecStart=/usr/bin/python3.11 /path/to/main.py
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
3. 起動・有効化
サービスを再読み込み。
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
起動・有効化。
sudo systemctl start myapp.service
sudo systemctl enable myapp.service
動作確認します。
sudo systemctl status myapp.service
下記のように出力されれば、うまく動作しています。
● myapp.service - My Python Application
Loaded: loaded (/etc/systemd/system/myapp.service; enabled; preset: enabled)
Active: active (running) since Mon 2026-07-13 10:03:45 JST; 10s ago
Invocation: 4f66d57b7d0d44a795921016534008c5
Main PID: 13716 (python3.11)
Tasks: 9 (limit: 3736)
Memory: 157.7M (peak: 157.8M)
CPU: 2.510s
CGroup: /system.slice/myapp.service
mq13716 /usr/bin/python3.11 /path/to/main.py
7月 13 10:03:47 ip-172-31-43-172 python3.11[13716]: handler = WebhookHandler(CHANNEL_SECRET)
7月 13 10:03:47 ip-172-31-43-172 python3.11[13716]: * Serving app 'main'
7月 13 10:03:47 ip-172-31-43-172 python3.11[13716]: * Debug mode: off