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
Systemd Files and Paths Unit files are stored in the /usr/lib/systemd directory and its subdirectories, while the /etc/systemd/ directory and its subdirectories contain symbolic links to the unit files necessary to the local configuration of the host. We recommend putting your scripts in /etc/systemd/system .
Description. The /etc/environment file contains variables specifying the basic environment for all processes. When a new process begins, the exec subroutine makes an array of strings available that have the form Name=Value. This array of strings is called the environment.
Sets the working directory for executed processes. If set to " ~ ", the home directory of the user specified in User= is used. If not set, defaults to the root directory when systemd is running as a system instance and the respective user's home directory if run as user.
If your service is running, you can use systemctl status <name>. service to identify the PID(s) of the service process(es), and then use sudo strings /proc/<PID>/environ to look at the actual environment of the process.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With