How to make Systemd a daemon application

What I wanted to do

I was deploy Python app to AWS EC2. and I wanted to make my app turn it into a daemon and restart it automatically if it falls.

Directory location

$ cd /etc/systemd/system
$ vi app.service

Code

[Unit]
Description=App

[Service]
ExecStart=/bin/bash -c '/usr/bin/python3 /home/ubuntu/APP/app.py'
WorkingDirectory=/home/ubuntu/APP/
Restart=always
Type=simple
User=ubuntu

[Install]
WantedBy=multi-user.target

Point

Here, even if the process goes down, it will restart automatically

Restart=always

Execution

$ sudo systemctl list-unit-files --type=service | grep app
$ sudo systemctl daemon-reload
$ sudo systemctl start app.service
$ sudo systemctl status app.service

Was about difficult

If it doesn’t work with permission issue, You can use something like sudo chmod 777 haha

藤沢瞭介(Ryosuke Hujisawa)
  • りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

未整理記事