By Alain Francois
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.
Systemd is now available on all recent Linux distributions. Log in to your Alibaba Cloud account and order an ECS instance:
Alibaba Cloud offers multiple Linux distributions:
Choose a Linux distribution based on your use case or requirements and create your virtual machine:
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=...
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
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
How to Manage Systemd Services and Units on ECS with the Systemctl Command
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
.
1,044 posts | 257 followers
Followliptanbiswas - July 15, 2018
Alibaba Clouder - May 11, 2018
Alibaba Clouder - July 25, 2019
Alibaba Clouder - October 2, 2018
Alex - June 21, 2019
Alibaba Clouder - June 5, 2019
1,044 posts | 257 followers
FollowAlibaba Cloud Linux is a free-to-use, native operating system that provides a stable, reliable, and high-performance environment for your applications.
Learn MoreSet up and manage an Alibaba Cloud multi-account environment in one-stop mode
Learn MoreMore Posts by Alibaba Cloud Community