在 Systemd 中引用其他环境变量 [英] Referencing Other Environment Variables in Systemd

查看:78
本文介绍了在 Systemd 中引用其他环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 systemd 中设置新环境变量时是否可以引用其他环境变量?

Is is possible to reference other environment variables when setting new ones in systemd?

[Service]
EnvironmentFile=/etc/environment
Environment=HOSTNAME=$COREOS_PRIVATE_IPV4
Environment=IP=$COREOS_PRIVATE_IPV4
Environment=FELIX_FELIXHOSTNAME=$COREOS_PRIVATE_IPV4

上面的代码似乎不起作用.

The above code does not seem to be working.

推荐答案

这确实是 unix & 的问题linux.但是:systemd 不会在Environment= 内进行环境变量扩展.来自 man systemd.exec:

This is really a question for unix & linux. But nevertheless: No, systemd will not perform environment variable expansion inside Environment=. From 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 部分下).

As you see from the example in the documentation $word just means $word no expansion will be performed. The specifiers that that man talks about are the %i, %n, %u, etc. They're in man systemd.unit (under their own man section).

另一方面,ExecStart= 及其衍生产品将执行环境变量扩展.在 ExecStart= 上使用环境变量是 systemd 中额外环境变量的常见解决方法.我相信,这也是为什么最近有这么多程序接受来自环境和命令行参数的相同参数的原因之一.

On the other hand ExecStart= and its derivatives will perform environment variable expansion. Using environment variables on the ExecStart= is the common workaround for extra environment variables in systemd. I believe, that is also one of the reasons why so many recent programs accept the same parameters from the environment and from command line parameters.

ExecStart= 中的扩展示例,来自 man systemd.service:

An example of expansion in ExecStart=, from 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 has its documentation spread out across several man's, but one gets used to them after a while.

这篇关于在 Systemd 中引用其他环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆