20

How does one set the PATH for non-login shells in CentOS 7?

Specifically, I have a systemd unit that needs binaries in /usr/local/texlive/2016/bin/x86_64-linux.

I attempted to set it in /etc/environment with PATH=/usr/local/texlive/2016/bin/x86_64-linux:$PATH but then my PATH was /usr/local/texlive/2016/bin/x86_64-linux:$PATH:/usr/local/sbin:/usr/sbin.

I created /etc/profile.d/texlive.sh with export PATH="/usr/local/texlive/2016/bin/x86_64-linux:${PATH}" but that only worked for login shells.

I looked at Set Path for all Users (Login and Non-login Shells) but the solution was already attempted above.

I looked at How to add a path to system $PATH for all users's non-login shell and login shell on debian but there's no accepted solution and I'm not sure I want to modify /etc/login.defs because it might get changed in an update.

3 Answers 3

23

The simplest answer is to set the PATH as part of your ExecStart command in the systemd Unit file. For example, if you currently have

ExecStart=/bin/mycmd arg1 arg2

then change it to

ExecStart=/bin/bash -c 'PATH=/new/path:$PATH exec /bin/mycmd arg1 arg2'

The expansion of $PATH will be done by bash, not systemd. Alternatives such as using Environment=PATH=/new/path:$PATH will not work as systemd will not expand the $PATH.

2
  • If I wrap the command this way AND use the profile.d file (I want the path added for login shells as well), would I still need to set the path in ExecStart? Or will that read from profile.d?
    – Agrajag9
    Commented Feb 27, 2017 at 15:33
  • I think you will still need to set the PATH in the ExecStart. The bash will probably only read from profile.d if it is a login shell. You could try adding --login before the -c to force this instead of settting the PATH, but you will be making bash run lots of setup that might not work well in the bare systemd environment, and give you errors in the journal log.
    – meuh
    Commented Feb 27, 2017 at 15:51
3

Unless you're using Environment=, EnvironmentFile= or PassEnvironment=, you should be using ExecSearchPath=.

man systemd.exec says concerning ExecSearchPath=:

Takes a colon separated list of absolute paths relative to which the executable used by the Exec*= (e.g. ExecStart=, ExecStop=, etc.) properties can be found. ExecSearchPath= overrides $PATH if $PATH is not supplied by the user through Environment=, EnvironmentFile= or PassEnvironment=. Assigning an empty string removes previous assignments and setting ExecSearchPath= to a value multiple times will append to the previous setting.

1
  • Note that 'ExecSearchPath' was added in systemd 250, so if your distro doesn't have that version (or newer) it's not available to you. Commented May 31, 2022 at 21:11
0

I've had success with the env program (systemd does not allow changing it via configuration files):

ExecStart=/usr/bin/env PATH=/usr/bin:/path1:/path2 mycmd arg1 arg2

Note that env does not start a new shell to execute the program, which may or may not be what you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .