Created
May 14, 2021 14:00
-
-
Save mikolasan/917455a42152eeab24a0bb3fcb549647 to your computer and use it in GitHub Desktop.
Python simple systemd service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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