Skip to content
November 27, 2014 / ftth

Systemd journal not showing python 3 print

The Systemd journal will print anything a service (unit) outputs to stdout or stderr. I wrote a simple python3 program but wasn’t seeing anything in the log that my program was printing (so, supposedly writing to stdout):

journalctl -f was only displaying the service-oriented information (start/restart…).

The solution is to flush stdout after each print:
import sys
print('Hello')
sys.stdout.flush()

Then journalctl shows the prints in real time, as expected:
Nov 27 19:31:09 rpi python[334]: Hello

Other solutions are to to run python with the -u flag (unbuffered output), re-open stdout in unbuffered mode or set the PYTHONUNBUFFERED environment variable.

Thanks to this stackoverflow article for the hint of this new python3 feature.

Advertisements

One Comment

Leave a Comment
  1. Denis / May 25 2017 5:46 am

    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: