14

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?

imbr
  • 103
  • 8
fileinsert
  • 449
  • 1
  • 3
  • 12
  • 3
    The answer is the same as at http://unix.stackexchange.com/questions/68059/ , http://unix.stackexchange.com/questions/206224/ , and http://unix.stackexchange.com/questions/164987/ . – JdeBP May 25 '16 at 16:02
  • @JdeBP Awesome! I don't feel too bad as the question titles wouldn't immediately lead me to it. However, any tips for better searching, or is this just experience talking? – fileinsert May 25 '16 at 20:20
  • Only experience of seeing the question before and [writing tools for working around such behaviour](http://unix.stackexchange.com/a/249801/5132). (-: – JdeBP May 25 '16 at 20:59

1 Answers1

15

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.

fileinsert
  • 449
  • 1
  • 3
  • 12
  • 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. – user1816847 Jan 21 '21 at 07: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: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED) – M1GEO Dec 01 '21 at 13:45
  • 1
    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 Jan 12 '22 at 13:49