If I export an environment variable in startup scripts does it persist?

9,999

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:
9,999

Related videos on Youtube

Jim
Author by

Jim

Updated on September 18, 2022

Comments

  • Jim
    Jim over 1 year

    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?

  • Zoredache
    Zoredache about 12 years
    Eh, that comment doesn't make sense.
  • Jim
    Jim about 12 years
    Never mind.You are right.In any case the problem was that tomcat doesn't accept environment props in conf files