Eric Eric - 1 month ago 23
Python Question

How do I properly redirect stdout/stderr from a systemd service on Raspbian?

I set up a service on Raspbian (Jessie) using systemd to make it start after boot. The daemon config looks like this:


ExecStart=/bin/sh -c "exec /home/pi/sources/ >> /home/pi/mydaemon.log 2>&1"


That redirection
isn't working. I have tried most of the options available to the
but they never end up printing my script's output to /var/log/daemon.log and
journalctl -u mydaemon.service
only shows messages about the service being started and stopped.

I'm not doing anything funny with file descriptors from within the script currently. I just want my
statements to show up somewhere I can read them. Any ideas?

(Just to be clear, the daemon does have to run as root. Could that have something to do with my printing problem?)

Answer Source

Normally you just run a service directly (make sure it's executable has a shebang line):


And use the default redirection of StandardOutput= to the systemd journal, so you can read the logs with journalctl -u mydaemon.service.

Systemd nicely controls the file growth and file rotation of the log for you.

It is unrelated that your service runs as root.

If you don't see any log output with the above, also check the entire log for nearby logs that might be attributed to your service:


This is necessary because there's a known issue when some last lines of logging just before a script exists might not be attributed to the script. This would be especially noticeable for a script that just runs a fraction of a second before exiting.