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 would suggest adding
stdout
andstderr
file in systemdservice
file itself.Referring : https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=
As you have configured it should not like:
It should be:
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:
NOTE: Make sure you create the directory already. I guess it does not support to create a directory.
You possibly get this error:
From the
systemd.exec(5)
man page:The
systemd.exec(5)
man page explains other options related to logging. See also thesystemd.service(5)
andsystemd.unit(5)
man pages.Or maybe you can try things like this (all on one line):
Short answer:
If you don't want the files to be cleared every time the service is run, use append instead:
If you have a newer distro with a newer
systemd
(systemd
version 236 or newer), you can set the values ofStandardOutput
orStandardError
tofile: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 ofStandardOutput
orStandardError
tofile:YOUR_ABSPATH_FILENAME
. Thefile:path
option is documented in the most recentsystemd.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).
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"
We are using Centos7, spring boot application with systemd. I was running java as below. and setting StandardOutput to file was not working for me.
Below workaround solution working without setting StandardOutput. running java through sh as below.