Journalctl only refreshing after restarting systemd service

daemongunicornjournalctlloggingsystemd

I have a systemd service with gunicorn. I have a print statement in a function.Problem is that, journalctl does not show the print output when i call the function but it shows the output after refresing the systemd service unit. Should it behave like this? What could be the solution for this?

Best Answer

This is due to Python's default behavior to buffer stdin, stdout and stderr. For a systemd file, include the following line within the same block as ExecStart=/path/to/gunicorn.

Environment=PYTHONUNBUFFERED=1

Refer to https://stackoverflow.com/questions/107705/disable-output-buffering for alternative ways to disable it.