Linux – Systemd python service buffers journalctl output even with -u

journaldlinuxpythonsystemd

I have a simple user systemd service configured like so:

[Unit]
Description=Bot
AssertPathExists=/home/mikel/discord-bot/

[Service]
WorkingDirectory=/home/mikel/discord-bot/
ExecStart=/home/mikel/anaconda3/bin/python -u bot.py parameters.json
Restart=always
RestartSec=3

[Install]
WantedBy=default.target

Essentially, all the service does is run a python script.

However, when I look at the journalctl for the service, I see that instead of logs appearing as things happen in the script, it is all flushed at once when the service is stopped (with the same timestamp for every line).

This suggests that some buffer is occuring somewhere that is preventing it from being written to journalctl immediately. However, even when I turn off Python's internal buffer with -u, the problem still occurs.

One solution I have found is to wrap it in the unbuffer command from the expect package, like so:

ExecStart=/usr/bin/unbuffer /home/mikel/anaconda3/bin/python -u bot.py parameters.json

However, while this works, it means that journalctl shows "unbuffer[PID]" instead of "python[PID]", and I want a solution that doesn't involve installing external packages for what seems like a simple task.

Is there some other way to remove these buffers so that the output is flushed to journalctl as it comes from python?

Best Answer

Through your experiment with unbuffer, you have established that the buffering is happening in the command you are running, not in systemd.

So the buffering is happening in the python code you are wrote. To solve that, you need to review the code you wrote or post the code here with a question.

Related Topic