在 Systemd 中引用其他环境变量
Referencing Other Environment Variables in Systemd
在systemd中设置新环境变量时是否可以引用其他环境变量?
[Service]
EnvironmentFile=/etc/environment
Environment=HOSTNAME=$COREOS_PRIVATE_IPV4
Environment=IP=$COREOS_PRIVATE_IPV4
Environment=FELIX_FELIXHOSTNAME=$COREOS_PRIVATE_IPV4
上面的代码似乎不起作用。
这真的是 unix & linux 的问题。但是尽管如此:No,systemd
不会在Environment=
内部进行环境变量扩展。来自 man systemd.exec
:
Environment=
Sets environment variables for executed processes. Takes a space-separated list of variable assignments. This
option may be specified more than once, in which case all listed variables will be set. If the same variable is
set twice, the later setting will override the earlier setting. If the empty string is assigned to this option,
the list of environment variables is reset, all prior assignments have no effect. Variable expansion is not
performed inside the strings, however, specifier expansion is possible. The $ character has no special meaning.
If you need to assign a value containing spaces to a variable, use double quotes (") for the assignment.
Example:
Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"
gives three variables "VAR1", "VAR2", "VAR3" with the values "word1 word2", "word3", "$word 5 6".
正如您从文档中的示例中看到的那样 $word
只是意味着 $word
不会执行任何扩展。 man
谈论的 说明符 是 %i
、%n
、%u
等。它们在 man systemd.unit
(在他们自己的 man
部分)。
另一方面ExecStart=
及其派生将执行环境变量扩展。在 ExecStart=
上使用环境变量是 systemd
中额外环境变量的常见解决方法。我相信,这也是其中一个为什么最近有这么多程序接受来自环境和命令行参数的相同参数的原因。
ExecStart=
中的扩展示例,来自 man systemd.service
:
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".
systemd
的文档分布在多个 man
中,但一段时间后就会习惯它们。
在我的例子中,将系统环境变量加载到 systemd
脚本中的唯一方法是将它们加载到脚本本身中,如下所述:
# Import our environment variables from systemd
for e in $(tr "[=10=]0" "\n" < /proc/1/environ); do
eval "export $e"
done
在systemd中设置新环境变量时是否可以引用其他环境变量?
[Service]
EnvironmentFile=/etc/environment
Environment=HOSTNAME=$COREOS_PRIVATE_IPV4
Environment=IP=$COREOS_PRIVATE_IPV4
Environment=FELIX_FELIXHOSTNAME=$COREOS_PRIVATE_IPV4
上面的代码似乎不起作用。
这真的是 unix & linux 的问题。但是尽管如此:No,systemd
不会在Environment=
内部进行环境变量扩展。来自 man systemd.exec
:
Environment=
Sets environment variables for executed processes. Takes a space-separated list of variable assignments. This
option may be specified more than once, in which case all listed variables will be set. If the same variable is
set twice, the later setting will override the earlier setting. If the empty string is assigned to this option,
the list of environment variables is reset, all prior assignments have no effect. Variable expansion is not
performed inside the strings, however, specifier expansion is possible. The $ character has no special meaning.
If you need to assign a value containing spaces to a variable, use double quotes (") for the assignment.
Example:
Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"
gives three variables "VAR1", "VAR2", "VAR3" with the values "word1 word2", "word3", "$word 5 6".
正如您从文档中的示例中看到的那样 $word
只是意味着 $word
不会执行任何扩展。 man
谈论的 说明符 是 %i
、%n
、%u
等。它们在 man systemd.unit
(在他们自己的 man
部分)。
另一方面ExecStart=
及其派生将执行环境变量扩展。在 ExecStart=
上使用环境变量是 systemd
中额外环境变量的常见解决方法。我相信,这也是其中一个为什么最近有这么多程序接受来自环境和命令行参数的相同参数的原因。
ExecStart=
中的扩展示例,来自 man systemd.service
:
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".
systemd
的文档分布在多个 man
中,但一段时间后就会习惯它们。
在我的例子中,将系统环境变量加载到 systemd
脚本中的唯一方法是将它们加载到脚本本身中,如下所述:
# Import our environment variables from systemd
for e in $(tr "[=10=]0" "\n" < /proc/1/environ); do
eval "export $e"
done