在systemd中使用环境变量的正确姿势

在systemd中使用环境变量的正确姿势

在systemd中使用环境变量的正确姿势

http://askubuntu.com/上找到
http://askubuntu.com/questions/614106/how-to-specify-an-environment-systemd-directive-containing

Example:

Environment="ONE=one" 'TWO=two two'
ExecStart=/bin/echo $ONE $TWO ${TWO}
This will execute /bin/echo with four arguments: one, two, two, and two two.

Example:

Environment=ONE='one' "TWO='two two' too" THREE=
ExecStart=/bin/echo ${ONE} ${TWO} ${THREE}
ExecStart=/bin/echo $ONE $TWO $THREE
This results in echo being called twice, the first time with arguments 'one', 'two two' too, , and the second time with arguments one, two two, too.
I tested this with the following service (note the quotes around the entire assignment):

[Unit]
Description=My Daemon

[Service]
Environment='CATALINA_OPTS=-Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current'
ExecStart=/bin/echo ${CATALINA_OPTS}

[Install]
WantedBy=multi-user.target
And got the desired output in journalctl:

Apr 26 08:19:29 laptop echo[28439]: -Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current
Of course, it would be simpler to use EnvironmentFile instead. Replacing the Environment with the following gave the same desired result:

EnvironmentFile=/tmp/foo
Where /tmp/foo contained (note the lack of quotes):

CATALINA_OPTS=-Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/xxxxxx