We will show you how to use ** variables ** in the Unit definition file when starting the service with the systemctl
command.
You can specify variables using ʻEnvironmentin the Unit definition file. Here, create a Unit definition file of
test.service` and check it.
TEST_VALUE1
to display1
and the variable TEST_VALUE2
to display2
./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.
You can use ʻEnvironmentFilein the Unit definition file to specify an environment variable file. Here, create a Unit definition file of
test2.service` and check it.
/etc/sysconfig/test2
TEST_VALUE1=display1
TEST_VALUE2=display2
is set to the environment variable file
/ etc / sysconfig / test2`./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.
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