Background Info:

I am trying to configure a Linux server running CentOS 7 for multiple users. After installing java I want to globally set the $JAVA_HOME environment variable for all users and add it to the $PATH.

To do this I went to the /etc/profile script that gets called on startup. I followed the advice at the top of the script:

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

I created my own /etc/profile.d/custom.sh script to configure the environment for java

custom.sh

# Java configuration
export JAVA_HOME=/usr/lib/jvm/java
export PATH=${JAVA_HOME}/bin:${PATH}

The Problem:

Whenever I log in, the /etc/profiles.d/custom.sh script (along with any other script I put in /etc/profiles.d) gets called twice and adds $JAVA_HOME to the $PATH twice.

I realized that this seems to happen because all of the scripts in /etc/profiles.d are called from both /etc/profile AND /etc/bashrc.

# This is in both /etc/profile and /etc/bashrc
for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

This really doesn't seem to be correct, but I did some digging and it turns out that this seems to just be the way it is...


Questions:

  1. Is this a bug in the CentOS 7 operating system?

  2. Is there a good workaround that I can use to create global environment settings without them being set twice? (I don't want to set them in /etc/environment because I can't use bash variables in that file.)

  • This is not the default on CentOS7. The snippet you posted, sourcing the stuff in /etc/profile.d is called in /etc/bashrc only if you are not in a login shell. To check if your stuff in /etc/profile.d is really executed twice, add echo "custom.sh" at the top of your script and do this for /etc/bashrc` and /etc/profile as well. You also migth want to add echo $PATH to at which point it gets doubled. – Thomas Dec 27 '17 at 19:22
  • @Thomas, I haven't modified /etc/profile or /etc/bashrc at all. What you said about /etc/bashrc is correct, but all of the scripts in /etc/profile.d still seem to be called twice anyways. – tjwrona1992 Dec 27 '17 at 19:27

I've been spawning hundreds of centos 7 the last 6 months, tweaking them in some ways, and I've never seen what you decribe. Like said Thomas, by default, /root/.bashrc doesn't call/source scripts in /etc/profile.d.

EDIT : Actually, /root/.bashrc DOES source /etc/prodile.d/*, by /etc/bashrc, and prevent the echos :

# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

Guess my recent problems where different, which I had to solve them fast and didn't dig enough. My apologies.

OLD ANSWER

1/ I guess it's really not a bug (what ISO did you get for your centos 7 ? Did you md5sum it ? Asking that question sounds actually weird to me)

2/ and then there's no workaround, but removing the code that makes scripts sourced twice ?

/etc/profiles.d seems the right place to put some scripts, if they're "silent", meaning defining some environment variables but not making echo's or calling some dynamic motd, or it will interact in some bad ways at some point, with other scripts you'll run (got the experience with netdata install for lately).

Some interesting points to read on unix.stackexchange.

  • It isn't called in /root/.bashrc, it's called in /etc/bashrc and I am beginning to realize that this particular issue may not show itself unless you are using CentOS 7 with some form of Desktop (I am using GNOME). The problem is showing up when I right click on the GNOME desktop and click open terminal window. If I type env | grep java into the terminal window I can see that it was added to the path twice. – tjwrona1992 Dec 28 '17 at 0:59
  • It seems like /etc/bashrc is called when I log in, and then it gets called again when I create a terminal window which is causing the path to be appended to twice. – tjwrona1992 Dec 28 '17 at 1:02
  • Also to answer your question, I got the "DVD" ISO and then just followed it through the normal installation steps. I did not md5sum it, but I don't think a bad ISO is the problem. – tjwrona1992 Dec 28 '17 at 1:05
  • But the source of your problem is not the Centos7 default. Just started a VM and it does not behave as you describe. I quickly did the echo entries. A normal open terminal window does only source ~/.bashrc, /etc/bashrc and /etc/profile.d/*, no /etc/profile and not double. Even if I use a login shell su - <username> nothing is sourced double. – Thomas Dec 28 '17 at 10:50
  • Also your statement that /root/.bashrc doesn't call those scripts in CentOS 7 is false (at least in my case). The default /root/.bashrc script that comes with a clean install calls /etc/bashrc which calls all of the scripts in profile.d. – tjwrona1992 Jan 2 at 18:07

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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