Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Please help me running below command from user2 account. I cannot figure out what's the issue with this.

[user2@localhost bin]# sudo -u user1 /opt/Tomcat/bin/shutdown.sh
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
[user2@localhost bin]$ echo $JAVA_HOME
/opt/Java_7

Entry for user2 in sudoers file is as below :

%user2 ALL=(user1) NOPASSWD:/opt/Tomcat/bin/startup.sh,/opt/Tomcat/bin/shutdown.sh

However the environment variables are set. I run the same script with root user and it works fine.

[root@localhost bin]# /opt/Tomcat/bin/shutdown.sh
Using CATALINA_BASE:   /opt/Tomcat/
Using CATALINA_HOME:   /opt/Tomcat/
Using CATALINA_TMPDIR: /opt/Tomcat//temp
Using JRE_HOME:        /opt/Java_7
Using CLASSPATH:       /opt/Tomcat//bin/bootstrap.jar:/opt/Tomcat//bin/tomcat-juli.jar

I have checked it further. JAVA_HOME is set for both the users.

[root@localhost bin]# pwd
/opt/Tomcat/bin
[root@localhost bin]# echo $JAVA_HOME
/opt/Java_7
[root@localhost bin]# su user1
[user1@localhost bin]$ echo $JAVA_HOME
/opt/Java_7
[user1@localhost bin]$ exit
[root@localhost bin]# su user2
[user2@localhost bin]$ echo $JAVA_HOME
/opt/Java_7

[user2@localhost bin]$ ps aux | grep java
user1     47066  0.1 10.9 1004608 77364 pts/2   Sl   Jun12   0:15 /opt/Java_7/bin/java -Djava.util.logging.config.file=/opt/Tomcat//conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/Tomcat//endorsed -classpath /opt/Tomcat//bin/bootstrap.jar:/opt/Tomcat//bin/tomcat-juli.jar -Dcatalina.base=/opt/Tomcat/ -Dcatalina.home=/opt/Tomcat/ -Djava.io.tmpdir=/opt/Tomcat//temp org.apache.catalina.startup.Bootstrap start
user2     49318  0.0  0.1 103240   840 pts/2    S+   02:11   0:00 grep java

[user2@localhost bin]$ sudo -u user1 /opt/Tomcat/bin/shutdown.sh
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
share|improve this question
    
That Conversation should rather be held here. Please check if you can run it when you log in as a different user, it's just for locating the problem. Also, try to use sudo -u user1 to echo your environment variable, see if that's different from when you actually log in as that user. –  private_meta Jun 13 at 9:38
    
Yes. I can run the it when i log in as user1. After adding the command in sudoers file for echoing variables , i am able to echo $JAVA_HOME. And it gives the expected output. –  newbie17 Jun 13 at 9:47

2 Answers 2

up vote 3 down vote accepted

To keep the JAVA_HOME environment variable when using sudo, you should add Defaults env_keep +="JAVA_HOME" to your sudoers file.

share|improve this answer

If you use sudo -u user1, then neither the JAVA_HOME path of user2, nor the path of root will be used, you will need to set the JAVA_HOME path of user1. From the way it looks, you did not set the JAVA_HOME path of user1

share|improve this answer
    
I have checked. JAVA_HOME is set for both the users. –  newbie17 Jun 13 at 9:14
    
Does the error persist when you use "sudo -u user1 -E"? Also, can you log in as user1 and run the command without sudo? –  private_meta Jun 13 at 9:18
    
when i run the command with -E it says: sudo: sorry, you are not allowed to preserve the environment. And this command should be run as sudo because we need to distribute these users to different teams(if it works) so that they have limitations. –  newbie17 Jun 13 at 9:27

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.