2

I'm finally trying to convert my old init.d management script to systemd, and have run into a snag that I hope someone else has found a solution to.

My application expects an argument for the name of the file it saves logging information to, and my init script generates that name at startup with this line:

LOGFILE=`/bin/date "+$MUDDIR/lib/log/runlog.%y%m%d-%H%M%S"`

How would I go about having such a construct get run so the value is available for use in ExecStart?

Can I do something as simple as

ExecStartPre=LOGFILE=`/bin/date "+${MUDDIR}/lib/log/runlog.%y%m%d-%H%M%S"`

or would it throw a fit at trying to spawn a shell there? If it runs the command as a subshell, the value would be thrown away.

Also, if I specify multiple ExecStartPre lines, will they be executed in order?

Thanks for any hints. The documentation is a bit light on details in some places, and I'm just trying to preserve my original functionality without being cheesy and just having it call my old script.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    See [Unix.SE: Dynamic logic in systemd service file](https://unix.stackexchange.com/questions/223973/dynamic-logic-in-systemd-service-file) – John Kugelman Sep 18 '18 at 12:27
  • 1
    See [Unix.SE: Dynamic variables in systemd service unit files](https://unix.stackexchange.com/questions/323914/dynamic-variables-in-systemd-service-unit-files) – John Kugelman Sep 18 '18 at 12:28

2 Answers2

2

I recommend letting systemd manage logging via its journal. Don't create a log file at all. Just have the application write to stdout/stderr. That way the logs will get stored alongside all the other applications' logs, they'll be searchable via journalctl, they'll show up in systemctl status <app>, they'll be rotated automatically, etc.

If the application absolutely requires a $LOGFILE environment variable you could use:

Environment=LOGFILE=/dev/stdout
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

I agree with OP's answer about letting journalctl handle logging. Rather than calling /dev/stdout in Environment attribute, use the following:

[Service]
...
...
StandardOutput=syslog
StandardError=syslog

This will put your logs into the system log, such as /var/log/messages and in journalctl. You can access the log using the service name:

journalctl -u myawesomeservice.service

As it seems, on newer (>238) systemd, you can set path to the log file.

StandardOutput=file:/path/to/logfile.log
StandardError=file:/path/to/logfile.err

Here is another SO question that provides much more info.

iamauser
  • 11,119
  • 5
  • 34
  • 52