[LINUX] How to use variables in systemd Unit definition files

We will show you how to use ** variables ** in the Unit definition file when starting the service with the systemctl command.

environment

1. Specify variables in the Unit definition file

You can specify variables using ʻEnvironmentin the Unit definition file. Here, create a Unit definition file oftest.service` and check it.

/etc/systemd/system/test.service


[Unit]
Description=TestService

[Service]
Environment=TEST_VALUE1=display1
Environment=TEST_VALUE2=display2
ExecStart=/bin/echo ${TEST_VALUE1} ${TEST_VALUE2}

Start with systemctl start test.

[root@CENTOS7 ~]# systemctl start test
[root@CENTOS7 ~]#

Check with journalctl -u test.

[root@CENTOS7 ~]# journalctl -u test
--Logs begin at day 2019-12-29 17:19:15 JST,end at day 2019-12-29 18:07:04 JST.
December 29 18:07:04 CENTOS7 systemd[1]: Started TestService.
December 29 18:07:04 CENTOS7 echo[1398]: display1 display2

The values of the variables TEST_VALUE1 and TEST_VALUE2, display1 and display2 are displayed.

2. Specify environment variables in the Unit definition file

You can use ʻEnvironmentFilein the Unit definition file to specify an environment variable file. Here, create a Unit definition file oftest2.service` and check it.

/etc/sysconfig/test2


TEST_VALUE1=display1
TEST_VALUE2=display2

/etc/systemd/system/test2.service


[Unit]
Description=Test2Service

[Service]
EnvironmentFile=/etc/sysconfig/test2
ExecStart=/bin/echo ${TEST_VALUE1} ${TEST_VALUE2}

Start with systemctl start test2.

[root@CENTOS7 ~]# systemctl start test2
[root@CENTOS7 ~]#

Check with journalctl -u test2.

[root@CENTOS7 ~]# journalctl -u test2
--Logs begin at day 2019-12-29 17:19:15 JST,end at day 2019-12-29 18:19:45 JST.
December 29 18:19:45 CENTOS7 systemd[1]: Started Test2Service.
December 29 18:19:45 CENTOS7 echo[1444]: display1 display2

The values of the variables TEST_VALUE1 and TEST_VALUE2, display1 and display2 set in / etc / sysconfig / test2 are displayed.

Supplement

Even if you specify TEST_VALUE1 = $ PATH etc. in the environment variable file / etc / sysconfig / test2, $ PATH will not be expanded as shown below.

/etc/sysconfig/test2


TEST_VALUE1=display1
TEST_VALUE2=$PATH
[root@CENTOS7 ~]# systemctl start test2
[root@CENTOS7 ~]# journalctl -u test2
--Logs begin at day 2019-12-29 17:19:15 JST,end at day 2019-12-29 18:25:04 JST.
December 29 18:25:04 CENTOS7 systemd[1]: Started Test2Service.
December 29 18:25:04 CENTOS7 echo[1457]: display1 $PATH

that's all

Recommended Posts

How to use variables in systemd Unit definition files
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use discrete values as variables in Scipy optimize
How to use calculated columns in CASTable
How to access environment variables in Python
How to dynamically define variables in Python
How to use Google Test in C
How to use functions in separate files Perl and Python versions
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to read CSV files in Pandas
How to use is and == in Python
How to use the C library in Python
Summary of how to import files in Python 3
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to check / extract files in RPM package
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
Use os.getenv to get environment variables in Python
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[For beginners] How to use say command in python!
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to load files in Google Drive with Google Colaboratory
How to use bootstrap in Django generic class view