Skip to content

Instantly share code, notes, and snippets.

@mikolasan
Created May 14, 2021 14:00
Show Gist options
  • Save mikolasan/917455a42152eeab24a0bb3fcb549647 to your computer and use it in GitHub Desktop.
Save mikolasan/917455a42152eeab24a0bb3fcb549647 to your computer and use it in GitHub Desktop.
Python simple systemd service
#!/usr/bin/env python3
from time import sleep
from threading import Thread
def do_the_thing():
n = 0
while True:
if n % 5 == 0:
print("thing")
else:
print("bad thing")
sleep(1)
n = n + 1
t = Thread(target=do_the_thing)
t.start()
# sudo vim /etc/systemd/system/my-python-project.service
[Unit]
Description=My Python Project
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/my-python-project
SyslogIdentifier=my-python-project
# Force the stdout and stderr streams to be unbuffered
Environment=PYTHONUNBUFFERED=1
ExecStart=/opt/my-python-project/main.py
# User with pip modules
User=root
Restart=always
RestartSec=20
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment