How to Pipe Output to a File When Running as a Systemd Service?

MichaelB76 picture MichaelB76 · Oct 6, 2015 · Viewed 30.8k times · Source

I'm having trouble piping the STDOUT & STDERR to a file when running a program as a systemd service. I've tried adding the following to the .service file:

ExecStart=/apppath/appname > /filepath/filename 2>&1

But this doesn't work. The output is ending up in /var/log/messages and is viewable using journalctl but I'd like a separate file.

I've also tried setting StdOutput=tty but can't find a way of redirecting this to a file.

Any help would be appreciated.

Answer

Evgeny Vereshchagin picture Evgeny Vereshchagin · Oct 9, 2015

systemd.service(5) says:

ExecStart=

Commands with their arguments that are executed when this service is started.

So, systemd runs your /apppath/appname with args >, /filepath/filename, 2>&1

Try:

ExecStart=/bin/sh -c '/apppath/appname > /filepath/filename 2>&1'