Skip to content

Instantly share code, notes, and snippets.

@sanjujosh
Last active October 22, 2019 00:36
Show Gist options
  • Save sanjujosh/54b7433768e4faf8b9e8a5ebc81663c9 to your computer and use it in GitHub Desktop.
Save sanjujosh/54b7433768e4faf8b9e8a5ebc81663c9 to your computer and use it in GitHub Desktop.
simple systemd service unit

Create the service unit

sudo touch /lib/systemd/system/sample.service
sudo chmod 664 /lib/systemd/system/sample.service

Reload the systemd

sudo systemctl daemon-reload

Check the unit is present

sudo systemctl list-unit-files | grep sample

Start the service

sudo systemctl start sample.service

Stop the service

sudo systemctl stop sample.service

Check the status

sudo systemctl status sample.service

Check the status with no logs truncaton

sudo systemctl -l status sample.service

Restart the service

sudo systemctl restart sample.service

Enable auto start the service on boot

sudo systemctl enable sample.service

Check logs

sudo journalctl -u sample.service

Follow logs like tail -f

sudo journalctl -f -u sample.service
[Unit]
Description=Sample Service
After=multi-user.target
[Service]
Type=simple
WorkingDirectory=/home/user/work
ExecStart=/home/user/work/venv/bin/python /home/user/work/main.py
User=user
# Always restart
Restart=always
# Slow python give em 2 seconds before restart
RestartSec=2
StartLimitIntervalSec=0
[Install]
# system wide service
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment