systemd write log to file code example
Example: systemd write log 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=.