How do I properly redirect stdout/stderr from a systemd service on Raspbian?
Normally you just run a service directly (make sure it's executable has a shebang line):
ExecStart=/home/pi/sources/mydaemon.py
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:
journalctl
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.
How to redirect output of systemd service to a file
I think there's a more elegant way to solve the problem: send the stdout/stderr to syslog with an identifier and instruct your syslog manager to split its output by program name.
Use the following properties in your systemd service unit file:
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<your program identifier> # without any quote
Then, assuming your distribution is using rsyslog to manage syslogs, create a file in /etc/rsyslog.d/<new_file>.conf
with the following content:
if $programname == '<your program identifier>' then /path/to/log/file.log
& stop
Now make the log file writable by syslog:
# ls -alth /var/log/syslog
-rw-r----- 1 syslog adm 439K Mar 5 19:35 /var/log/syslog
# chown syslog:adm /path/to/log/file.log
Restart rsyslog (sudo systemctl restart rsyslog
) and enjoy! Your program stdout/stderr will still be available through journalctl (sudo journalctl -u <your program identifier>
) but they will also be available in your file of choice.
Source via archive.org
systemd service redirect stdout to custom filename
systemd cannot generate the file name dynamically. But you can use bash
redirection and date
to create such a logfile.
[Service]
ExecStart=/bin/bash -c "/bin/mybin >/my/path/filename-$(date %%y-%%d-%%m).log"
How to log to journald (systemd) via Python?
python-systemd has a JournalHandler you can use with the logging framework.
From the documentation:
import logging
from systemd.journal import JournalHandler
log = logging.getLogger('demo')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")
Related Topics
How to Remove a Single Quotes from a List
Discord.Py | Add Role to Someone
Python-3: Why This Following Code Returns None in Print Statement
Python Pip Install Fails: Invalid Command Egg_Info
Python 3D Polynomial Surface Fit, Order Dependent
Python | Count Number of False Statements in 3 Rows
Run Multiple Python File Concurrently
Python - Outputting Variables to Txt File
Converting Exponential to Float
Plot Two Histograms on Single Chart With Matplotlib
How to Get the Name of an Object
How to Convert Signed to Unsigned Integer in Python
Numpy: How to Pick Rows from Two 2D Arrays Based on Conditions in 1D Arrays
How to Create an Automatically Updating Gui Using Tkinter
I Want to Multiply Two Columns in a Pandas Dataframe and Add the Result into a New Column
How to Add Thousand Separator to Numbers in Python Pandas Dataframe
Using Opencv to Overlay Transparent Image Onto Another Image