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.


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.

| improve this answer | |
  • 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 Feb 27 '17 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 Feb 27 '17 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.