What is the right way to set PATH variable in a systemd
unit file? After seeing a few examples, I tried to use the format below, but the variable doesn't seem to expand.
在systemd单元文件中设置PATH变量的正确方法是什么?在看到一些示例之后,我尝试使用下面的格式,但是这个变量似乎没有展开。
Environment="PATH=/local/bin:$PATH"
I am trying this on CoreOS with the below version of systemd.
我正在CoreOS上试用systemd的以下版本。
systemd 225
-PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK -SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT -GNUTLS -ACL +XZ -LZ4 +SECCOMP +BLKID -ELFUTILS +KMOD -IDN
2 个解决方案
#1
18
You can't use EnvVars
in Environment
directives. The whole Environment=
will be ignored. If you use EnvironmentFile=
, then the specified file will be loaded without substitution. So PATH=/local/bin:$PATH
would be exactly that, and this is probably not what you want.
在环境指令中不能使用envars。整个环境=将被忽略。如果您使用环境文件=,那么指定的文件将在没有替换的情况下被加载。PATH=/local/bin:$PATH就是这个,这可能不是你想要的。
Under CentOS7 the following works.
在CentOS7下面的作品。
# /etc/systemd/system/nagios.service.d/env.conf
[Service]
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
> sudo systemctl daemon-reload
> sudo systemctl restart nagios
> sudo cat /proc/28647/environ
...
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
...
#2
1
You can use the EnvironmentFile=
directive in the units
section to set environment variables.
您可以在units部分中使用environmental file = directive来设置环境变量。
Just put the variables as key=value
pairs and it will work.
只需将变量作为键=值对,就可以了。
The runtime just 'source's whatever file you specify.
运行时就是你指定的任何文件的源文件。
You can create the file using the write_files
directive.
您可以使用write_files指令创建文件。
#1
18
You can't use EnvVars
in Environment
directives. The whole Environment=
will be ignored. If you use EnvironmentFile=
, then the specified file will be loaded without substitution. So PATH=/local/bin:$PATH
would be exactly that, and this is probably not what you want.
在环境指令中不能使用envars。整个环境=将被忽略。如果您使用环境文件=,那么指定的文件将在没有替换的情况下被加载。PATH=/local/bin:$PATH就是这个,这可能不是你想要的。
Under CentOS7 the following works.
在CentOS7下面的作品。
# /etc/systemd/system/nagios.service.d/env.conf
[Service]
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
> sudo systemctl daemon-reload
> sudo systemctl restart nagios
> sudo cat /proc/28647/environ
...
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
...
#2
1
You can use the EnvironmentFile=
directive in the units
section to set environment variables.
您可以在units部分中使用environmental file = directive来设置环境变量。
Just put the variables as key=value
pairs and it will work.
只需将变量作为键=值对,就可以了。
The runtime just 'source's whatever file you specify.
运行时就是你指定的任何文件的源文件。
You can create the file using the write_files
directive.
您可以使用write_files指令创建文件。