1

I'm invoking a python3 module using systemd (version: systemd 232) on debian stretch. The problem is, if the python module raises an exception, the exception message (stderr) and some prior prints (stdout) are not output to the journal before the module exits with failure.

The service configuration looks like this:

[Unit]
Description=Some Service
After=network.target

StartLimitIntervalSec=3
StartLimitBurst=2

[Service]
User=foo

PermissionsStartOnly=true
ExecStartPre=-/bin/mkdir -p /var/run/foo
ExecStartPre=/bin/chown user:user /var/run/foo/

Type=simple
PIDFile=/var/run/foo/foo.pid
ExecStart=/usr/local/bin/foo_continual
Restart=always
RestartPreventExitStatus=0
RestartSec=2

StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

foo_continual is a bash script that sets up a python virtual environment and then runs the python module:

#!/bin/bash
source /home/foo/environment/bin/activate || exit 1
python -u -m foo.main || exit 1

The script contains some print() calls. I even tested flush=True to one of them, and it contains an exception that should be printed to stderr and cause the program to exit with a non-0 exit code. The problem is that the journald logs do not contain the full output, including the error message:

Dec 01 18:32:07 TESTING systemd[1]: foo.service: Service hold-off time over, scheduling restart.
Dec 01 18:32:07 TESTING systemd[1]: Stopped Some Service.
Dec 01 18:32:07 TESTING systemd[1]: Started Some Service.
Dec 01 18:32:08 TESTING foo_continual[50903]: Print some stuff...
Dec 01 18:32:08 TESTING systemd[1]: foo.service: Main process exited, code=exited, status=1/FAILURE
Dec 01 18:32:08 TESTING systemd[1]: foo.service: Unit entered failed state.
Dec 01 18:32:08 TESTING systemd[1]: foo.service: Failed with result 'exit-code'.
Dec 01 18:32:10 TESTING systemd[1]: foo.service: Service hold-off time over, scheduling restart.
Dec 01 18:32:10 TESTING systemd[1]: Stopped Some Service.
Dec 01 18:32:10 TESTING systemd[1]: Started Some Service.
Dec 01 18:32:10 TESTING systemd[1]: foo.service: Main process exited, code=exited, status=1/FAILURE

It should print more text, including an error to stderr after "Print some stuff". How can I get jorunald to record all of the output before the program exits?

1

The only solution that I found is not a good one but it works. In the bash file, sleep before exiting to give journald chance to record the output:

#!/bin/bash
function fail {
    sleep 2
    exit 1
}
source /home/foo/environment/bin/activate || fail
python -u -m foo.main || fail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.