Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am installing Tomcat on a RHEL5 machine, and am having some trouble starting it, because JAVA_HOME isn't set. Tomcat is being started using

sudo /sbin/service tomcat start

and runs under the user tomcat. When I am logged in as myself or as the root user, JAVA_HOME is properly set (using /etc/profile.d/java.sh). I am guessing that this isn't being properly passed through by sudo.

Can someone point me in the direction of the change I need to make to get this variable either passed through by sudo or picked up by the tomcat user? I believe there are a number of ways.

Thanks in advance

Rich

share|improve this question

5 Answers 5

up vote 2 down vote accepted

One way to do it is to add JAVA_HOME to the tomcat start up script, which should be in /etc/init.d

share|improve this answer

mazianni's answer is correct.

More info:

For a non-interactive shell /etc/profile is not read and therefore /etc/profile.d/*.sh aren't read either.

JAVA_HOME is set in your interactive shell. sudo strips out most environment variables. You could configure the env_keep in /etc/sudoers to allow JAVA_HOME through but if you ever set tomcat to automatically start this method will no longer work. (Hence mazianni's solution is the best.)

share|improve this answer

Try sudo -E <your command>. This preserves (most of) the environment. See man sudo for more information.

share|improve this answer

You can temporarily set a variable before execution in BASH. This will be available to the command immediately following.

JAVA_HOME=/path/to/java sudo /sbin/service tomcat start

share|improve this answer

Read man sudoers.

Using visudo (never edit /etc/sudoers directly!), you can choose to set !env_reset to globally allow (most) environment variables to pass through sudo, or add to env_keep which specific variables should be kept.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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