Skip to content

Run Scripts using SystemD

Create an application execute script file

/run-myapp.sh
#!/bin/bash

/usr/bin/python3.7 -m uvicorn app:app --host 0.0.0.0 >> /app.log 2>&1

Create a systemd service unit file

/etc/systemd/system/myapp.service
[Unit]
Description=My Python Application
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/
ExecStart=/run-myapp.sh
Restart=always
StandardInput=null
StandardOutput=file:/app.log
StandardError=file:/app.log

[Install]
WantedBy=multi-user.target

Create a log file

touch /app.log

Reload SystemD

systemctl daemon-reload

Start application

systemctl start myapp

Get application status

systemctl status myapp