LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 07-25-2010, 12:04 AM   #1
Mechanic
LQ Newbie
 
Registered: Jun 2003
Posts: 24

Rep: Reputation: 15
Environment variable being set twice


I'm setting my CLASSPATH in /etc/profile.d/jre.sh. In a login shell, everything is fine. In an xterm window, the CLASSPATH consists of two of every intended entry. In jre.sh I am doing a

Code:
export CLASSPATH=$CLASSPATH:/many/paths/to/jars
So I'm guessing this is getting run twice in the xterm case. Can someone explain what's going on here and what I should do to remedy this? TIA
 
Old 07-25-2010, 12:20 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
could you please post output of echo $CLASSPATH.

And even if it is coming twice, I dont think there would be any problem because of it. So, you can ignore it.

This generally happens when, you have exported the same variable (here CLASSPATH) twice.

Like,
Code:
export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin
So this will show everything twice.
 
Old 07-28-2010, 04:50 PM   #3
Mechanic
LQ Newbie
 
Registered: Jun 2003
Posts: 24

Original Poster
Rep: Reputation: 15
In an xterm, CLASSPATH looks something like

Code:
/path/to/a.jar:/path/to/b.jar:/path/to/c.jar:/path/to/a.jar:/path/to/b.jar:/path/to/c.jar:
The fact that in a login shell the classpath looks fine tells me that I'm not doing anything stupid like exporting it twice. I suspect that /etc/profile.d/jre.sh is being run a second time when I open an xterm. I could fix this by changing

Code:
export CLASSPATH=$CLASSPATH:/many/paths/to/jars
to

Code:
export CLASSPATH=/many/paths/to/jars
But I'd like to know why this is happening just the same. Indeed before this export statement CLASSPATH is an empty string, but I'm not convinced this is generally true. Unless someone knows this is true . . .
 
Old 07-28-2010, 05:29 PM   #4
ljb643
Member
 
Registered: Nov 2003
Posts: 526

Rep: Reputation: Disabled
What is the window manager / login environment? (kde, xfce, other?) Have you customized any of the session startup scripts?

My guess is that /etc/profile is running twice. Once from the Xsession startup file (or equivalent), at login time. The environment of that process is inherited by all your other session processes. Then your xterm is running /etc/profile itself, perhaps because it thinks it is a login shell. This would double up CLASSPATH, but also some others like MANPATH would have double JAVA_HOME/man entries. (Does MANPATH have this?)
 
Old 07-28-2010, 07:31 PM   #5
Mechanic
LQ Newbie
 
Registered: Jun 2003
Posts: 24

Original Poster
Rep: Reputation: 15
I'm using gnomeslack for login and xfce for a window manager. I don't believe I've done any customization. MANPATH is not doubled up like CLASSPATH. As I've said, I put my CLASSPATH setting in /etc/profile.d/jre.sh but this script is run by /etc/profile, so I believe that /etc/profile is being run twice.
 
Old 07-29-2010, 09:19 AM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question

Quote:
Originally Posted by Mechanic View Post
In an xterm, CLASSPATH looks something like

Code:
/path/to/a.jar:/path/to/b.jar:/path/to/c.jar:/path/to/a.jar:/path/to/b.jar:/path/to/c.jar:
Why do you have duplicate entries for a.jar, b.jar and c.jar.

I believe this is the problem.
 
Old 07-29-2010, 12:01 PM   #7
Mechanic
LQ Newbie
 
Registered: Jun 2003
Posts: 24

Original Poster
Rep: Reputation: 15
That's a good question! That's why I asked it in my original post!
 
Old 07-29-2010, 12:28 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
search for files containing "CLASSPATH"

You might try grepping for instances of "CLASSPATH=" or "export CLASSPATH" in files throughout your system. An example might be:
Code:
grep -Erl 'CLASSPATH=' / 2>/dev/null
So it searched recursively from the / of the filesystem, and shows the filename where the occurrence happens. Check each file for instances of CLASSPATH="${CLASSPATH}:...blah..blah.." or CLASSPATH="$CLASSPATH:..blah..blah.."

I don't seem to have much in the way of "CLASSPATH" being set anywhere in my system, but as a sample of what the output would resemble, here I searched for "export PATH" and got expected results (and a few dozen others!):
Code:
root@reactor: grep -Erl 'export PATH' /etc 2>/dev/null
/etc/profile.d/kde.sh
/etc/profile.d/jre.sh
/etc/serqt_pcidir/serqt_pci
/etc/profile
/etc/profile~
/etc/profile.SAVE-CUSTOMIZED
/etc/profile.sva-orig
/etc/zprofile
/etc/rc.d/rc.autofs

<--snip-->
Tried this too:
Code:
grep -Erl 'export PATH' /home/sasha/ 2>/dev/null
but got nothing.
 
Old 08-01-2010, 03:11 PM   #9
AppDeb
LQ Newbie
 
Registered: Aug 2010
Posts: 10

Rep: Reputation: 0
There is a bug in the profile.d scripts that appends some paths instead of replacing them. I think it will be fixed in the next slackware release (or current soon).

In your example, each time something executes "source /etc/profile" CLASSPATH gets appended instead of replaced.

You have to prevent your shells from executing (for second time) "source /etc/profile", check your profile scripts (.bashrc, .bash_profile etc) so they don't do that.

The correct solution is to make the profile.d scripts create PATHs from scratch and not append.

Last edited by AppDeb; 08-01-2010 at 03:16 PM.
 
Old 08-01-2010, 05:35 PM   #10
Mechanic
LQ Newbie
 
Registered: Jun 2003
Posts: 24

Original Poster
Rep: Reputation: 15
GrapefruiTgirl: I tried doing a search for 'CLASSPATH=' but either it takes a really long time or my computer hung. Either way I did not get any output from the grep.

AppDeb: I'm glad to hear that the scripts in /etc/profile.d are supposed to create paths and not append to them and that is what I ended up doing: removing the append that I was doing on CLASSPATH. It looks OK now.

My .bashrc and .bash_profile do not source /etc/profile so that wasn't the problem. And now that it looks OK I don't think I'm affected by the bug you spoke of.

Unfortunately, I still don't understand what was going on. My jre.sh in /etc/profile.d must have been called a second time when I open a terminal window but I don't see how.
 
Old 08-02-2010, 06:53 PM   #11
ljb643
Member
 
Registered: Nov 2003
Posts: 526

Rep: Reputation: Disabled
You might want to try adding some logging to jre.sh to confirm this. For example, add this

(date; echo "jre.sh is running in $$") >> /tmp/log

This will record the date and process ID. Log out and back in and look at the /tmp/log file. At least, it will confirm the file is running twice, and it might help track down why. (You may see two entries with the same PID, or a parent/child pair.)
 
Old 08-03-2010, 12:11 AM   #12
AppDeb
LQ Newbie
 
Registered: Aug 2010
Posts: 10

Rep: Reputation: 0
Mechanic:

If your xterm starts as a "login shell" it sources /etc/profile (and .profile in your home) on start, and that is the second time in your case, because Xorg has already been started from a login shell.

You have to start xterm as an "interactive shell" so that it doesn't source /etc/profile on startup.

The xfce terminal and the gnome terminal by default start an interactive shell (but in the options you have a choice for login shell). I don't know what xterm does.

update:

From the xterm manual page

Quote:
+ls This option indicates that the shell that is started should not
be a login shell (i.e., it will be a normal “subshell”).
So you can try starting it with "xterm +ls" and the profile should not be sourced for second time.

Last edited by AppDeb; 08-03-2010 at 12:23 AM.
 
Old 08-03-2010, 03:22 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Fixing this problem by modifying /etc/profile.d/jre.sh to set CLASSPATH (rather than add to it) will cause breakage if and when some other script, run before jre.sh, needs to modify CLASSPATH.

One possible reason for the problem is that /etc/profile is run as normal when startin a login shell (including a graphical login shell) and then, when a non-login interactive shell is started (when starting xterm), ~/.bashrc is run as normal and sources /etc/profile -- resulting in running /etc/profile.d/jre.sh twice.

If this is the case, there are two solutions: either remove the sourcing of /etc/profile from .bashrc (preferred because it may not only be jre.sh+CLASSPATH that is going wrong) or modifying jre.sh to only add the directory to CLASSPATH if CLASSPATH does not already contain it.

Details of bash' startup file usage are in the man page and GNU Bash Reference

Last edited by catkin; 08-03-2010 at 03:25 AM. Reason: Added ", run before jre.sh,"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Set environment variable prabuinet Linux - Newbie 2 12-07-2008 12:44 AM
can't set environment variable baosheng Linux - Software 3 04-03-2007 01:53 AM
How to set an environment variable royeo Linux - Newbie 1 12-01-2006 12:59 AM
How to set a environment variable in rh? yenonn Linux - Newbie 11 02-21-2003 10:24 PM
how do you set the environment variable CC? mr.moto Linux - General 0 12-17-2001 09:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration