0

This article (https://news.ycombinator.com/item?id=9793466) makes a case for to sparsely use forking in systemd. Following this advice I try the following python service script:

import time

def run():
    with open("/tmp/pysystemd/svc.out","w") as f:
        while True:
            print("***")
            f.write("+++\n")
            time.sleep(0.5)

run()

with the following systemd.service script:

[Unit]
Description=Simple zebra service
After=multi-user.target

[Service]
Type=Simple
#ExecStart = /usr/bin/python /tmp/pysystemd/svc.py > /tmp/pysystemd/std.out
ExecStart = /bin/bash -c '/usr/bin/python /tmp/pysystemd/svc.py > /tmp/pysystemd/std.out'
WorkingDirectory = /tmp/pysystemd

[Install]
WantedBy=multi-user.target

Though file /tmp/pysystemd/std.out is created it doesn't contain the expected output... Help appreciated.

stustd
  • 303
  • 1
  • 10

1 Answers1

1

Your output problem is probably buffered output. I don't see any output buffer flushes in your program.

See How to flush output of Python print?

Also, redirecting your output to your own log file isn't really the systemd way to do it. If you let your standard output and error go to defaults they will be in the journal and you can get it with journalctl -u my-service

Community
  • 1
  • 1
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131