2

I am trying to redirect logs to a file using systemd. I have tried adding below statements to the service.

StandardOutput=file:/tmp/test1.txt
StandardError=file:/tmp/test2.txt

But those files are not generating. Is there anything I am missing here? Is there any kernel flags which has to be enabled for this?

I am using systemd version 216 in my device.

A R
  • 193
  • 2
  • 10

1 Answers1

5

We added file: support for StandardOutput and StandardError in version 236 (https://github.com/systemd/systemd/pull/7198) -- I'm afraid your version is too old.

A reasonable workaround in your version would be to create a wrapper script which sets up stdout and stderr to your liking, and invoke that instead.

Chris Down
  • 122,090
  • 24
  • 265
  • 262
  • Yeah, I was suspecting the same and I cant upgrade to newer version. But I didnt get the the workaround you are talking here. If you dont mind, Can you show me an example or elaborate a little more? – A R Oct 10 '19 at 06:09
  • @AR A shell script containing `exec foo > /tmp/test1.txt 2>/tmp/test2.txt` should be enough. Then you can specify that as `ExecStart` in the unit. – Chris Down Oct 11 '19 at 17:10
  • Note: the file: option doesn't clear the original logfile. Instead it just overwrites the start of it, leaving bits of old log file at the end, which can be extremely confusing. This is the behaviour as of 237. – Owl Mar 01 '22 at 11:52