Centos – Globally change path for all users, even in cron

centoscronpathsettings

We're setting up an SGE cluster with CentOS 6. My sysadmin is installing applications that are not installed via RPM (i.e. via other means like make install) should go in a non-standard directory, in this case something like /share/apps/install/bin/. The path for this is currently added to most sessions (login, qlogin, etc) via /share/apps/etc/environment.sh which is called by /etc/bashrc. environment.sh also appends some stuff to the PERL5LIB.

The problem that I'm running into is that the /share/apps/install/bin is not added to some instances, e.g. things called out of a crontab.

I know I can manually and explicitly set PATH=/bin:/usr/bin:/blah/blah:... within my personal crontab or within any given script or crontab entry, but what I'm hoping is that there's a setting somewhere outside of /etc/profile or /etc/bashrc that would put the non-standard .../bin directory into all PATHs for all users.

Best Answer

Add a file with the value you want the PATH to have in /etc/profile.d. These files are setup to be sourced by shells such as Bash, Csh Zsh, or tcsh.

Example

We needed to have the following value added to our PATH.

/usr/local/share/bin

So we created a file, /etc/profile.d/ourstuff.sh, with the following line in it:

export PATH=/usr/local/share/bin:$PATH

Files with the extension .sh are sourced by shells such as Bash and Zsh. Files with the extension .csh are sourced by Csh and tcsh.

EDIT #1 - Follow-up

OP asked the following follow-up question.

Yes, but what about cron jobs? Is there a way to get the path even there? cron doesn't seem to call /etc/profile or /etc/bashrc.

To which I responded:

Correct it doesn't nor will it. You need to set the SHELL=/bin/bash in cron to override the default shell (typically /bin/sh). Also you can set the this for user crons, BASH_ENV="$HOME/.bashrc", and this for system crons, BASH_ENV="/root/.bashrc". Would be one way around this.

I would highly suggest that you not do this. Let the scripts that need a specific environment, set it up themselves. Don't try to solve every problem at the global level!

Related Question