15

I have a custom service and have explicitly called for all stdout & stderr to be sent to syslog in the config file, however only some of the output appears in both syslog and the journal (they are consistent).

I my desperation I have done the following in the service files:

StandardOutput=syslog+console
StandardError=syslog+console

The service is a python script and I write to stdout using the print statement. These items seem to be lost to the ether, while other command outputs write correctly both to syslog and journald. If I run the script interactively everything appears in stdout as expected.

What is lacking in my knowledge?

3

1 Answer 1

16

JdeBP's comment was the correct solution:

Systemd Python service not sending all output to syslog

The solution was to add the -u option to the interpreter to make standard streams send their output unbuffered.

5
  • 1
    For me adding -u wasn't a good option. Another way to do it is to call sys.stdout.flush() whenever stdout needs to be written. Commented Jan 21, 2021 at 7:07
  • For what it's worth, you can also set PYTHONUNBUFFERED to a non-empty string which is equivalent to specifying the -u option in Python2 and Python3 (Source: docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED)
    – M1GEO
    Commented Dec 1, 2021 at 13:45
  • 2
    you can set a shebang like #!/usr/bin/python3 -u on your python script, set exec permissions and call it directly from your .service
    – imbr
    Commented Jan 12, 2022 at 13:49
  • How come buffering leads to lost output? Shouldn't buffered output be sent to stdout all at once (e.g., once the script terminates) rather than being lost altogether? Commented May 24 at 7:38
  • @MagnusLindOxlund If the program crashes, or doesn't exist cleanly before the buffer is flushed data can be lost. Also, syslog should be unbuffered as you want an accurate timestamp and logs in order. However this was from 2.5 years ago and now I tend to flush as required.
    – fileinsert
    Commented Aug 9 at 5:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .