How to redirect output of systemd service to a file

systemd standardoutput=append
systemd service write stdout to file
systemd service file
systemd service log rotation
systemd unit redirect stdout
redirect journalctl to file
systemd output to console
systemd logging

I am trying to redirect output of a systemd service to a file but it doesn't seem to work:

[Unit]
Description=customprocess
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/binary1 agent -config-dir /etc/sample.d/server
StandardOutput=/var/log1.log
StandardError=/var/log2.log
Restart=always

[Install]
WantedBy=multi-user.target

Please correct my approach.

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

redirect systemd service logs to file, StandardOutput/Error systemd directives do not support appending to files. If you want to maintain file log between service restarts and just  How to redirect systemd started service output to a file? Ask Question Asked 2 years, @Rinzwind Thanks, but I want to redirect the output to a file.

If you have a newer distro with a newer systemd (systemd version 236 or newer), you can set the values of StandardOutput or StandardError to file:YOUR_ABSPATH_FILENAME.


Long story:

In newer versions of systemd there is a relatively new option (the github request is from 2016 ish and the enhancement is merged/closed 2017 ish) where you can set the values of StandardOutput or StandardError to file:YOUR_ABSPATH_FILENAME. The file:path option is documented in the most recent systemd.exec man page.

This new feature is relatively new and so is not available for older distros like centos-7 (or any centos before that).

How to redirect systemd service log to file? : linuxadmin, You can add StandardOutput and StandardError to your unit files and specify where the output should go. If you need to ship the logs to a remote system, then​  To specify a file, there's another cleaner option, as indicated by the documentation: The fd option connects the output stream to a single file descriptor provided by a socket unit. A custom named file descriptor can be specified as part of this option, after a ":" (e.g. "fd:foobar"). – orion Jan 23 '17 at 10:25

You possibly get this error:

Failed to parse output specifier, ignoring: /var/log1.log

From the systemd.exec(5) man page:

StandardOutput=

Controls where file descriptor 1 (STDOUT) of the executed processes is connected to. Takes one of inherit, null, tty, journal, syslog, kmsg, journal+console, syslog+console, kmsg+console or socket.

The systemd.exec(5) man page explains other options related to logging. See also the systemd.service(5) and systemd.unit(5) man pages.

Or maybe you can try things like this (all on one line):

ExecStart=/bin/sh -c '/usr/local/bin/binary1 agent -config-dir /etc/sample.d/server 2>&1 > /var/log.log' 

How can I output logs to a file from the content of a service with , As explained here, it turns out that you can't redirect output directly within ExecStartPre , ExecStart , or ExecStopPost commands - systemd will interpret the > or  Restart rsyslog. systemctl restart rsyslog.service. I am trying to redirect output of a systemd service to a file but it doesn't seem to work: [Unit] Description=customprocess After=network.target [Service] Type=forking ExecStart=/usr/local/bin/binary1 agent -config-dir /etc/sample.d/server StandardOutput=/var/log1.log StandardError=/var/log2.log Restart=always [Install] WantedBy=multi-user.target.

I would suggest adding stdout and stderr file in systemd service file itself.

Referring : https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=

As you have configured it should not like:

StandardOutput=/home/user/log1.log
StandardError=/home/user/log2.log

It should be:

StandardOutput=file:/home/user/log1.log
StandardError=file:/home/user/log2.log

This works when you don't want to restart the service again and again.

This will create a new file and does not append to the existing file.

Use Instead:

StandardOutput=append:/home/user/log1.log
StandardError=append:/home/user/log2.log

NOTE: Make sure you create the directory already. I guess it does not support to create a directory.

Redirecting output from a systemd service (python script) to a log file , Continuing from here: Force rc.local to wait for network, I have been working on implementing my own systemd service to run a custom python  In specific, from SysV to systemd. I meet problems of logs missing. I have a service start script in RedHat6, say scriptA. It will output some log info to console if I execute it to start a service. Then with systemd, I use "systemctl start" to execute the scriptA, logs from scriptA can only be seen in journal.

If for a some reason can't use rsyslog, this will do: ExecStart=/bin/bash -ce "exec /usr/local/bin/binary1 agent -config-dir /etc/sample.d/server >> /var/log/agent.log 2>&1"

Redirecting the output of service in Ubuntu to Log files, EDIT 1: I have also read that if systemd version is 236 or above, the log file absolute path can be provided directly to the StandardOutput and  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.

Linux Logging with Systemd - The Ultimate Guide To Logging, A unit file basically describes a resource and tells systemd how to activate that resource. The naming standard for a unit file is <resource_name>.<unit_type>. The  You may also want to take a look at View stdout/stderr of systemd service. What you already can do with your current setup, is use journalctl -u yourunitname > yourlogfile_for_yourunitname to direct the whole journal output for your unit "yourunitname" into a file.

Systemd redirect stdout to log file, Stdout/stderr can be redirected by the unit file, even though there is no really How to redirect output of systemd service to a file Assume logs are already put to​  Continuing from here: Force rc.local to wait for network, I have been working on implementing my own systemd service to run a custom python script.Everything seems to be working, except that I cannot seem to get the script stdout to redirect to a log file.

16.04 - Systemd print logs to external log file, It automatically redirects both STDIN and STDOUT there either as well as handling Just leave the log file redirection out of your ExecStart= line. The docs in man systemd.service explain that the redirection syntax is not  Thanks for contributing an answer to Unix & Linux Stack Exchange! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

Comments
  • Not working for me on Ubuntu 16.04. journalctl -u still works but nothing gets sent to the specified file.
  • This works great on Debian stretch, however it complains that ~ is deprecated and stop should be used instead. Also note that the second line can be shortened to & stop if the two come after each other.
  • With systemd 236 or newer you can also write directly to a file using StandardOutput=file:/some/path github.com/systemd/systemd/pull/7198
  • I got this working by changing /etc/rsyslog.d/<newfile>.conf contents to: :programname, isequal, "<your program identifier>" /var/log/somelog.log Here's documentation on the rsyslog filters: rsyslog.com/doc/v8-stable/configuration/filters.html And here's docs on the properties like programname: rsyslog.com/doc/master/configuration/properties.html
  • I had problem using this configuration until I found that rsyslog has its own user syslog and it has to have write access to the logs location. So use chown accordingly. Hope this helps somebody.
  • Not working in ubuntu 1604 in 2018-03-20. The systemd version in ubuntu 1604 is only 229.
  • Thanks, you said very clear. I just can not believe the systemd in ubuntu 1604 can not redirect output to a file just by config.I have to use the sh way to solve this problem.
  • @bronzeman the feature request wasn't closed until 2017, while Ubuntu 16.04 came out in 2016. In a given major release of Ubuntu (e.g. 16.04, 16.10, 17.04, etc.), Ubuntu maintains ABI compatibility in its core system packages. So they won't upgrade systemd (or the Linux kernel, or glibc, or anything) unless it maintains the same ABI as when the Ubuntu version was first released.
  • FWIW: I've searched a bit but this feature doesn't appear to have provisions for log rotation, such as function to reopen the log file, with one having to use the likes of copytruncate in logrotate.
  • Among the options, logging into the systemd journal is recommend. You view just the logs for your process in the journal by using journalctl -u your-unit-name.