redirect systemd service logs to file

I was trying to run flume on Ubuntu 16.04 as systemd service and have following in /etc/systemd/system/flume-ng.service

[Unit]
Description=Apache Flume

[Service]
ExecStart=/usr/bin/nohup /opt/flume/current/bin/flume-ng agent -c /etc/flume-ng/conf -f /etc/flume-ng/conf/flume.conf --name a1 &
ExecStop=/opt/flume/current/bin/flume-ng agent stop

[Install]
WantedBy=multi-user.target

I tried adding following lines

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

which didn’t work for me.
I did run systemctl daemon-reload and systemctl restart flume-ng

anyone know how this works ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Redirect

The following works only in systemd v236 and newer:

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

as documented here.

In case of systemd older than v236, 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'

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=.

Method 2

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

Method 3

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

Method 4

I hope this helps someone.
for Ununtu

[Unit]
Description=name-service
After=syslog.target
After=mysql.service
Requires=mysql.service
[Service]
Type=idle
PIDFile=/path_to_PID/****.pid
WorkingDirectory=/path_to_directory
User=root
Group=root
ExecStart=****.py
Restart=always
TimeoutStartSec=1min
RestartSec=1min
KillMode=process
StandardOutput=append:/path_to_log/service.log
StandardError=append:/path_to_log/service_error.log
[Install]
WantedBy=multi-user.target

for CENTOS

[Unit]
Description=name-service
After=syslog.target
After=mysql.service
Requires=mysql.service
[Service]
Type=idle
PIDFile=/path_to_PID/****.pid
WorkingDirectory=/path_to_directory
User=root
Group=root
ExecStart=****.py  >>/path_to_log/log.log
Restart=always
TimeoutStartSec=1min
RestartSec=1min
KillMode=process
[Install]
WantedBy=multi-user.target

“After” and “Requires” of necessity.
Attension “User” and “Group”

Another option: add logging inside executed scripts.

Method 5

If you don’t get any output at all you should test your script first.

For example a Python script will buffer the output streams (but only when they are piped, not when you test on the console). So you will not see any log messages until the buffer is flushed.

Specify the -u switch ( python3 -u script) or set the PYTHONUNBUFFERED variable to avoid this:

Environment=PYTHONUNBUFFERED=1

Method 6

[Unit]
Description=ngrokd server

[Service]
Type=forking
User=ngrok
Group=ngrok
ExecStart=/bin/sh -c '/usr/local/ngrok/bin/ngrokd 
    -tlsCrt /usr/local/ngrok/tls/srv-ngrok.cert 
    -tlsKey /usr/local/ngrok/tls/srv-ngrok.key 
    -domain="ioio.cool" 
    -httpAddr=:8480 
    -httpsAddr=:8433 
    -tunnelAddr=:8422 >> /var/log/ngrokd.log 2>&1 &'
Restart=always
KillMode=process

[Install]
WantedBy=multi-user.target

You can see man systemd.exec, search keywords StandardOutput, and man systemd.service, serach keywords COMMAND LINES

This syntax is intended to be very similar to shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood.
Specifically, redirection using “<“, “<<“, “>”, and “>>”, pipes using “|”, running programs in the background using “&”, and other elements of shell syntax are not
supported.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x