Setting Environment Variables for a systemd Service: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[Systemd_Concepts#Environment|systemd Concepts]]
* [[Systemd_Operations#Setting_Environment_Variables_for_a_systemd_Service|systemd Operations]]


* [[systemd#Configuration|systemd]]
=Overview=


=Overview=
This document describes the procedure of configuring a <code>systemd</code> 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 and the Configuration File=


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


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.
A file that contains environment variables could be named <code>env.conf</code>. <code>myservice.conf</code> is also an acceptable name.


For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:
{{Note|Note that if the configuration directory exists and is empty, the service will be disabled.}}


=Configuration File Content=
<syntaxhighlight lang='bash'>
[Service]
[Service]
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
Environment="JAVA_HOME=/opt/java"
Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"
Environment="JBOSS_HOME=/opt/jboss"
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.
</syntaxhighlight>

Latest revision as of 22:22, 19 August 2023

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 file. 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.

Configuration File Content

[Service]
Environment="JAVA_HOME=/opt/java"
Environment="JBOSS_HOME=/opt/jboss"