Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

systemd: "Environment" directive to set PATH

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 
like image 981
ϹοδεMεδιϲ Avatar asked Feb 16 '16 17:02

ϹοδεMεδιϲ


People also ask

Where do I put systemd environment files?

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 .

What is Environmentfile?

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.

What is working directory in systemd?

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.

How do I find systemd environment variables?

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.


1 Answers

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 ... 
like image 171
xoryves Avatar answered Sep 29 '22 21:09

xoryves