How To Redirect Output Of Systemd Service To A File (Ubuntu).

Lets start from the service file which is located at the following directory: /etc/systemd/system/ (You can use the "cd" command - cd /etc/systemd/system/).

The second step is to edit our service file, for that open the file for editing using "vim" editor. Use the following command sudo vim your_service.service.

Add the following lines under the [Service] at your service file.

SyslogIdentifier=[our service identifier] -> Just as it is(No brackets, no quotes)

In order to close the vim editor press ":" then write wq.

Now we have to create config file for the rsyslog so it would know to process our service's logs.

Access /etc/rsyslog.d/ directoy and create there a new config file name it at the same name you named you service: our_service.conf

Then place the following content at the file:

if $programname == '[our service identifier]' then /var/log/our_service.log & stop

Now give syslog the ability to write into our file:

chown syslog:adm /var/log/our_service.log 

And the final step is to restart the rsyslog service:

sudo systemctl restart rsyslog

6 thoughts on “How To Redirect Output Of Systemd Service To A File (Ubuntu).

  1. It is really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top