2

I have the following problem:

I am using Tomcat 6.0.32 and Java JDK 6.0_26. I have installed it successfully and the Tomcat start page is visible in the browser at port 8080.

I have also created $CATALINA_HOME/setenv.sh script and put some webapp-specific environment variables in it (along with the CATALINA_HOME, JAVA_HOME and CLASSPATH).

I have created a new user "tomcat", set a new home directory for him, and also passwd-ed it.

This script is being sourced from within a init script I created to start and stop Tomcat automatically on reboot. I do not use the standart startup.sh and shutdown.sh found in $CATALINA_HOME, but rather then jsvc daemon starter, so I can use port 8080 from a non-root process (Tomcat itself).

The actual problem is that, after restarting Tomcat my webapp does not receive or see the environment variable I set in setenv.sh and so it won't start.

I have tried to put the environment variable definition in various places:

  • .bashrc in the tomcat home directory
  • /etc/init.d/tomcat script
  • $CATALINA_HOME/bin/setenv.sh
  • $CATALINA_HOME/webapps/myapp/META-INF/context.xml

to no avail, after start of Tomcat my webapp does not see the required environment variables.

My question is - what the heck am I doing worng? Any suggsetions? How am I supposed to transfer env vars to an webapp if the setenv.sh does not work? What could make this mechanism faulty (allegedly this is the way to hand env vars to webapps)?

Here is the startup script I worte:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          allfaweb
# Required-Start:    $syslog $apache $apache2 $httpd
# Should-Start:
# Required-Stop:     $syslog $apache $apache2 $httpd
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: ALLFAweb service
### END INIT INFO

ALLFAWEB_BIN=/install/apache-tomcat-6.0.32-allfaweb/bin/jsvc
test -x $ALLFAWEB_BIN || { echo "$ALLFAWEB_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Check for existence of setenv.sh file and read it
ALLFAWEB_CONFIG=/install/apache-tomcat-6.0.32-allfaweb/bin/setenv.sh
test -r $ALLFAWEB_CONFIG || { echo "$ALLFAWEB_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }


. /etc/rc.status
rc_reset

. /install/apache-tomcat-6.0.32-allfaweb/bin/setenv.sh;


case "$1" in
    start)
        echo -n "Starting ALLFAweb ";
        $ALLFAWEB_BIN \
           -user tomcat \
           -home $JAVA_HOME \
           -Dcatalina.home=$CATALINA_HOME \
           -pidfile $ALLFAWEB_PID \
           -outfile $CATALINA_HOME/logs/catalina.out \
           -errfile $CATALINA_HOME/logs/catalina.err \
           -cp $CLASSPATH org.apache.catalina.startup.Bootstrap

        rc_status -v
        ;;
    stop)
        echo -n "Shutting down ALLFAweb "
        $ALLFAWEB_BIN \
            -stop \
            -pidfile $ALLFAWEB_PID \
            org.apache.catalina.startup.Bootstrap

        rc_status -v
        ;;
    restart)
        $0 stop
        $0 start
        rc_status
        ;;
    status)
        echo -n "Checking for service ALLFAweb ";
        /sbin/checkproc $ALLFAWEB_BIN
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
rc_exit

The system I am using is a SUSE SP2:

# uname -a
Linux testmachine 3.0.51-0.7.9-default #1 SMP Thu Nov 29 22:12:17 UTC 2012  x86_64 x86_64 x86_64 GNU/Linux

Any help would be highly appreciated! Thanks in advance :)

  • you don't need to be root to start tomcat on 8080 – Will Jan 15 '13 at 16:03
  • 1
    Are you saying that this script works properly on boot but not afterward? When you "restart" Tomcat and things don't work, how are you restarting Tomcat? – Christopher Schultz Jan 15 '13 at 16:45
  • 1
    Note that `bin/setenv.sh` is expected to be called from `bin/catalina.sh`. At that point, `CATALINA_HOME` and `CATALINA_BASE` have already been established. After calling `bin/setenv.sh`, `bin/catalina.sh` will blank-out any `CLASSPATH` you have set. However you solve this problem, I suggest that you *not* use `bin/setenv.sh` to set `CATALINA_HOME`, `CATALINA_BASE`, and `CLASSPATH`. – Christopher Schultz Jan 15 '13 at 16:48
  • @Christopher: The init script works properly after boot, and after that too, i can start/stop/restart it as many times as I want. Where should I export CATALINA_HOME and CATALINA_BASE when I am starting Tomcat via jsvc? Directly as parameters to jsvc? In that case, how I make sure that the variables in setenv.sh get exported to the enviroment of the webapp? Maybe there is a sepcific way to export custom env vars with jsvc, but I haven't found anything about that in the jsvc command line options. Thanks for the feedback! :) – Konstantin Boyanov Jan 15 '13 at 18:29
  • 1
    You don't need CATALINA_HOME and CATALINA_BASE for jsvc: you specify them as parameters right there in the `start)` case with your `-D` parameter. What is it exactly that you are trying to do? Does your webapp attempt to read environment variables? Why? – Christopher Schultz Jan 15 '13 at 22:10
  • Yes, I am trying to pass two enviroment variables to the webapp, one poiting to a configuration directory and the other pointing to the BIRT reporting engine folder. The strange thing is that thhis worked two days ago when Tomcat was started as the root user, but now when I created the new "tomcat" user and chown-ed all the installation files of Tomcat to it, the enviroment var is no longer passed to the webapp properly. Is passing env vars to Tomcat webapps wrong? – Konstantin Boyanov Jan 16 '13 at 08:28

2 Answers2

1

You should be using system properties and not environment variables. Check the source for Tomcat's bin/daemon.sh script, which passes all of the standard variables to Tomcat when it is launched.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • I cannot find bin/daemon.sh in my Tomcat 6.0.32 installation directory. Is it version specific, or do I create it myself? – Konstantin Boyanov Jan 16 '13 at 08:37
  • Confirmed - when the apache install directory is owned by root (and all the files in it), and Tomcat is started by the root user (root owns the tomcat processes) everything works fine, the webapp gets its environment variable. If, however, The Tomcat install files are owned by the user I created, OR the Tomcat container is started by the user I created, nothing works and I get a good old 404 :) So what is the standart way of running Tomcat as a non-root user? – Konstantin Boyanov Jan 16 '13 at 10:24
  • 1
    Looks like `bin/daemon.sh` is only provided with Tomcat 7. That's why I gave you a link to it so you could read it. – Christopher Schultz Jan 16 '13 at 16:43
  • 1
    I've never run Tomcat as root, and I've never had any problem doing so. This is the first time you've mentioned 404 responses. Can you be more clear? This question seems to be turning into an ongoing consulting engagement. – Christopher Schultz Jan 16 '13 at 16:45
1

I found it. Allegedly the reason is that at first I started Tomcat 6 as root, and then changed the ownership of files to user "tomcat". The root process had written files all over the file system wiht the appropriate permissions, that is, the "tomcat" user could not read them.

The default shell of the "tomct" user was /bin/sh and not /bin/bash

I also deleted the files in $CATALINA_HOME/work and renamed the directory to which my custom env var was pointing to (and the env var accordingly).

There were also some logfiles generated from log4j in the / dicrectory, those are gone too and now everything works as expected :)

Thanks for all the feedback, you kept me going during these 3 days of frustration :)

Cheers!