[CentOS] Redirect output from python script with systemd

Sat Jul 7 21:36:28 UTC 2018
Jonathan Billings <billings at negate.org>

On Jul 7, 2018, at 3:28 PM, C. L. Martinez <carlopmart at gmail.com> wrote:
> I need to redirect an output from python script that runs as a systemd service. I have tried to redirect its output in Exec statement without luck. I have tried to use StandardOutput and StandardError options also, but outpu log goes to /var/log/messages.
> 
> Any tip?
> 

Ideally, you should be using python’s “logging” library[1] to write to a file if you don’t want it going to the journal. But if you must make systemd handle it, use StandardOutput=file:/path/to/logfile[2]. Make sure your service has access to that file to write. 

You can’t use shell redirects in Exec lines since systemd units aren’t shell scripts. You can, of course have the service include:
Exec=/use/bin/bash -c “pythonscript > /var/log/script.log”



1. https://docs.python.org/2/library/logging.html
2. https://www.freedesktop.org/software/systemd/man/systemd.exec.html

—
Jonathan Billings <billings at negate.org>