×
Community Blog How to Set Environment Variables in a Systemd Service

How to Set Environment Variables in a Systemd Service

This article explains how to set environment variables in a systemd service.

By Alain Francois

What Is Systemd Service?

Systemd is the new service manager in Linux systems. It is the initialization system adopted by all the latest Linux distributions. Also, it is event-based (like Upstart), which makes its operation flexible and dynamic. There are a number of different methods that can be used to set environment variables for a systemd service. The method used can vary depending on the Linux distribution and the version of the distribution.

Order Your ECS Instance

Systemd is now available on all recent Linux distributions. Log in to your Alibaba Cloud account and order an ECS instance:

1

Alibaba Cloud offers multiple Linux distributions:

2

Choose a Linux distribution based on your use case or requirements and create your virtual machine:

3

Systemd Files and Paths

Systemd uses the systemctl tool, which allows you to configure the services launched at startup. It introduces the "unit" concepts represented by some configuration files. Unit files are stored in the /usr/lib/systemd directory and its subdirectories, while the /etc/systemd/ directory and its subdirectories contain symbolic links to the unit files necessary to the local configuration of the host. We recommend putting your scripts in /etc/systemd/system. The systemd script should follow some sections:

  • [Unit] defines unit behavior and dependencies with other units:
[Unit]
Description=... 
After=...
  • [Service] defines the unit type. There are several different startup types to consider when writing a custom service file, such as dbus, forking, oneshot, and simple. You can also define how to start the daemon:
[Service]
Type=...
ExecStart=...
ExecReload=...
  • [Install] contains information on the installation of the unit used:
[Install]
WantedBy=...

Using the EnvironmentFile

With systemd, you can set your environment variables in a text file. It is called EnvironmentFile because it is used to support loading environment variables that can be used in unit files. This means you can allow multiple environment variables to be added to the service via a simple file:

[Unit]
Description=description-about-the-service
After=network.target

[Service]
Type=simple  
Restart=always
User=username-the-service-should-run-as
EnvironmentFile=/path/to/environment/file1
EnvironmentFile=/path/to/environment/file2
EnvironmentFile=-/path/to/environment/file3
ExecStart=/path/to/the/script/to/start

[Install]
WantedBy=multi-user.target

You can see a - before the path of the "environment file 3". This lets systemd ignore errors if the file does not exist. It should be specified more than once, in which case all specified files are read.

You need to reload the daemon with the command for the modification to take effect:

$ sudo systemctl daemon-reload

Using the Environment

Instead of using an environment file, you can set the environment variable in the service file directly. You will need to indicate the value of the environment variable:

[Unit]
Description=description-about-the-service
After=network.target

[Service]
Type=simple  
Restart=always
User=username-the-service-should-run-as
Environment=MY_ENV1=value1
Environment=MY_ENV2=value2
Environment=MY_ENV3=value3
ExecStart=/path/to/the/script/to/start

[Install]
WantedBy=multi-user.target

You need to reload the daemon with the command:

$ sudo systemctl daemon-reload

Related Articles

How to Manage Systemd Services and Units on ECS with the Systemctl Command

Conclusion

Almost all major Linux distributions have adopted systemd. Since systemctl is an important component of systemd, you will often need to use this command to control various parts of your system. This utility is just one of many provided by the systemd software suite. Here are a few examples of other programs included in this suite: journalctl, networkctl, hostnamectl, timedatectl, and localectl.

0 0 0
Share on

Alibaba Cloud Community

1,057 posts | 259 followers

You may also like

Comments