Search code examples
servicefilenamessystemd

systemd service redirect stdout to custom filename


I am working with systemd services in order to start an application. Stdout should be redirected to a file containing the current date (when the service was started). Logging to a file works fine, however, I don't know how to provide the date for the filename within the service. Any ideas?

...
[Service]
ExecStart=/bin/mybin
StandardOutput=file:/my/path/<filename should contain date>.log
...

Solution

  • systemd cannot generate the file name dynamically. But you can use bash redirection and date to create such a logfile.

    [Service]
    ExecStart=/bin/bash -c "/bin/mybin >/my/path/filename-$(date %%y-%%d-%%m).log"