redirect systemd service logs to file

Redirect

StandardOutput=file:/var/log/flume-ng/log1.log
StandardError=file:/var/log/flume-ng/log2.log

as documented here: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=

Note that this way the whole log files contents will be overwritten each time service restarts.

Append

If you want to maintain file log between service restarts and just append new logged lines to it:

# Works only in systemd v240 and newer!
StandardOutput=append:/var/log/flume-ng/log1.log
StandardError=append:/var/log/flume-ng/log2.log

In case of systemd older than v240, you can use:

ExecStart=/bin/sh -c 'exec /usr/bin/my_binary [arguments] >>/var/log/flume-ng/log1.log 2>>/var/log/flume-ng/log2.log'

exec means that shell program will be substituted with my_binary program after setting up redirections without forking. So there will be no difference from running my_binary directly after ExecStart=.


ExecStart=/usr/bin/nohup …

This is wrong. Remove it. This service is not running in an interactive login session. There is no controlling terminal, or session leader, to send a hangup signal to it in the first place.

ExecStart=… &

This is wrong. Remove it. This is not shell script. & has no special shell-like meaning, and in any case would be the wrong way to start a service.

StandardOutput=/var/log/flume-ng/log1.log
StandardError=/var/log/flume-ng/log2.log

These are wrong. Do not use these. systemd already sends the standard output and error of the service process(es) to its journal, without any such settings in the service unit. You can view it with

journalctl -e -u flume-ng.service


For logging to file without overwriting (system.d version 240+ required):

StandardOutput=append:/var/log/flume-ng/log1.log
StandardError=append:/var/log/flume-ng/log2.log

or

StandardOutput=append:/var/log/flume-ng/log.log
StandardError=inherit

Tags:

Linux

Ubuntu