Setting Environment Variables for a systemd Service: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:
This document describes the procedure of configuring a <tt>systemd</tt> service by injecting custom environment variables into its environment. The document assumes we're configuring a service named "myservice".
This document describes the procedure of configuring a <tt>systemd</tt> service by injecting custom environment variables into its environment. The document assumes we're configuring a service named "myservice".


=Create the Configuration Directory=
=Create the Configuration Directory and the Configuration File=


Create a directory /etc/systemd/system/myservice.service.d, and inside that directory create a file whose name ends in .conf, and in this file you can add to or override any part of the unit shipped by the distribution.
Create a directory <tt>/etc/systemd/system/myservice.service.d</tt>. Inside that directory create a <tt>.conf</tt>. Multiple files are allowed. These files contains configuration overrides for any part of the unit shipped by the distribution.
 
A file that contains environment variables could be named <tt>env.conf</tt>. <tt>myservice.conf</tt> is also an acceptable name.
 
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:<br>Note that if the configuration directory exists and is empty, the service will be disabled.<br><br>
</blockquote>


For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:
For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:
Line 16: Line 22:
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"
Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"
Also note that if the directory exists and is empty, your service will be disabled! If you don't intend to put something in the directory, ensure that it does not exist.

Revision as of 18:07, 16 February 2016

Internal

Overview

This document describes the procedure of configuring a systemd service by injecting custom environment variables into its environment. The document assumes we're configuring a service named "myservice".

Create the Configuration Directory and the Configuration File

Create a directory /etc/systemd/system/myservice.service.d. Inside that directory create a .conf. Multiple files are allowed. These files contains configuration overrides for any part of the unit shipped by the distribution.

A file that contains environment variables could be named env.conf. myservice.conf is also an acceptable name.


Note that if the configuration directory exists and is empty, the service will be disabled.

For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:

[Service] Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN" Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"