0

This seems like it should be simple, but I am struggling to make it work. I am trying to set up a systemd service so output from a logging program (varnishncsa) that outputs log entries from memory can be piped to cronolog. The only way I have been able to make it work is in the foreground. I am trying to make it work as a systemd service. The script that takes over the foreground is:

#!/bin/bash
/usr/bin/varnishncsa -F '%{X-Real-IP}i %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i"' -q "ReqHeader:Host ~ '(^|\.)example\.com$'" -C |/usr/sbin/cronolog "/example/varnish_access_log.%Y-%m-%d"

The systemd service setup I am trying is:

[Unit]
Description=Example website Varnish Cache HTTP accelerator NCSA logging daemon
After=varnish.service

[Service]
RuntimeDirectory=varnishncsa
Type=forking
PIDFile=/run/varnishncsa/varnishncsa-example.pid
User=varnish
Group=varnish
ExecStart=/usr/local/bin/varnishncsa-example.sh
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

Which is based on the provided systemd service for running as a daemon and writing to a file (I can't run it directly as a daemon, using -D and -P, and pipe the output):

[Unit]
Description=Varnish Cache HTTP accelerator NCSA logging daemon
After=varnish.service

[Service]
RuntimeDirectory=varnishncsa
Type=forking
PIDFile=/run/varnishncsa/varnishncsa.pid
User=varnish
Group=varnish
ExecStart=/usr/bin/varnishncsa -a -w /var/log/varnish/varnishncsa.log -D -P /run/varnishncsa/varnishncsa.pid
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

I have tried many options to make it run in the background, but can't get anything to work, and can't find anything online that deals with this situation specifically. Probably because systemd is still quite new, or others doing this have more idea what they are doing!

Any help greatly appreciated.

1

You have removed the -D option so the program will no longer daemonise itself. You have also removed the -P option to save the process id in a file. So your Unit should not continue with say it is a forking type, and should not give a bogus pid file which you are not filling. Try Type=simple instead, and remove PIDFile=... and ExecReload=... since your script does not handle rotating the log file.

2
  • Thanks, I'll try that. The script does handle rotating the logfile, that's what the piping to cronolog is for.
    – user194553
    Apr 7 '17 at 19:43
  • Have tried it now. Thanks so much, this works perfectly :D
    – user194553
    Apr 7 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy