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.

When I create a startup script in /etc/init.d and do an export of a variable does this variable persist and is "viewable" to the started process?
To be specific I have the following script to start tomcat in init.d which I start mannually with start:

#!/bin/bash
RETVAL=$?
export JRE_HOME=/home/jre
export PATH=/home/jre/bin/:${PATH}
export CATALINA_HOME=/home/apache-tomcat-7.0.25
case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
            /bin/su root $CATALINA_HOME/bin/startup.sh
        fi
        ;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
            /bin/su root $CATALINA_HOME/bin/shutdown.sh
        fi
        ;;
 restart)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
            /bin/su root $CATALINA_HOME/bin/shutdown.sh
        fi
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
            /bin/su root $CATALINA_HOME/bin/startup.sh
        fi
        ;;        
 *)
        echo $"Usage: $0 {start|stop}"
        exit 1
        ;;
esac
exit $RETVAL   

Now in Tomcat I am using connection pooling and and when I use an absolute url in then all works fine. I.e.: url="jdbc:h2:file:/home/apache-tomcat-7.0.25/webapps/myDB;SCHEMA=mySchema"

But if I use a relative url i.e. $CATALINA_HOME in the url i.e. url="jdbc:h2:file:$CATALINA_HOME\webapps\myDB;SCHEMA=mySchema" this does NOT work and I get JNDI exception.
Note that in my script I have exported $CATALINA_HOME to be /home/apache-tomcat-7.0.25

It seems like the $CATALINA_HOME is not set for the started instance of Tomcat.
Could this be the case? Anyone has an idea why the url with the environment variable does not work?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

When a new process is created the environment is copied from the parent. So any environment variables you set in the script before you start another process should exist.

Some programs like su/sudo will do some filtering of the environment depending on how they are called, as a security measure. So your init script is calling su which is starting $CATALINA_HOME/bin/startup.sh, which I guess is actually doing the Tomcast startup. Something is probably removing or ignoring the variable.

I do wonder why you are calling using su root from within an init script. Init scripts run as root by default, there should be no reason why you need to become root again. If I were you I would drop what seems to be a worthless call to su.

Just to help you troubleshoot. As root, on Linux, when you have /proc mounted, you can look what the current environment is.

For example if tomcat had a process ID of 1234 then you would want to take a look at the contents of /proc/1234/environ. Anything that is set should be there.

share|improve this answer
    
Eh, that comment doesn't make sense. –  Zoredache Feb 29 '12 at 10:06
    
Never mind.You are right.In any case the problem was that tomcat doesn't accept environment props in conf files –  Jim Feb 29 '12 at 10:29

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.