systemd: “Environment” directive to set PATH

2020-05-30 04:10发布

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.

Environment="PATH=/local/bin:$PATH"

I am trying this on CoreOS with the below version of systemd.

systemd 225
-PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK -SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT -GNUTLS -ACL +XZ -LZ4 +SECCOMP +BLKID -ELFUTILS +KMOD -IDN

2条回答
Rolldiameter
2楼-- · 2020-05-30 04:32

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.

Under CentOS7 the following works.

# /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
...
查看更多
ゆ 、 Hurt°
3楼-- · 2020-05-30 04:34

You can use the EnvironmentFile= directive in the units section to set environment variables.

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.

查看更多
登录 后发表回答