Skip to content

Instantly share code, notes, and snippets.

@girol
Last active March 10, 2020 16:35
Show Gist options
  • Save girol/fa671681897a194e64b86e8302b01506 to your computer and use it in GitHub Desktop.
Save girol/fa671681897a194e64b86e8302b01506 to your computer and use it in GitHub Desktop.
Python script SystemD example
from time import sleep
import os
hi = os.environ.get('HELLO')
seconds = 1
while(True):
sleep(1)
print("logging...", seconds, "Env Var: ", hi)
seconds +=1
HELLO='Chambering The Chamber'
[Unit]
Description=SystemD Test Service!
After=network.target
[Service]
User=chambers
Group=chambers
EnvironmentFile=/etc/default/superlogger
WorkingDirectory=/home/my_user/my_project/systemd-templates
ExecStart=/usr/bin/python3 -u app.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
@girol
Copy link
Author

girol commented Mar 10, 2020

Notes

  • The -u parameter starts Python in unbuffered mode, chaining the print() output to journalctl
  • The WorkingDirectory is where the script app.py is located
  • Place the superlogger environment file inside /etc/default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment