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 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.
Related Topics
How to Prevent a Background Process from Being Stopped After Closing Ssh Client in Linux
What Does Set -E Mean in a Bash Script
How to Pass Command Output as Multiple Arguments to Another Command
How to Change Permissions For a Folder and Its Subfolders/Files
How Does "Cat ≪≪ Eof" Work in Bash
Get Exit Code of a Background Process
Linux Blocking Vs. Non Blocking Serial Read
How to Generate a Core Dump in Linux on a Segmentation Fault
How to Search For a Multiline Pattern in a File
Bash Script Prints "Command Not Found" on Empty Lines
What Happens to an Open File Handle on Linux If the Pointed File Gets Moved or Deleted
Confused About Stdin, Stdout and Stderr
How to Add a Default Include Path For Gcc in Linux
Convert Dos Line Endings to Linux Line Endings in Vim
How to Kill a Process Running on Particular Port in Linux
How to Get Process Id of Background Process