We help IT Professionals succeed at work.

Tomcat RC startup script, works manually but not upon boot

JBurner
JBurner asked
on
450 Views
Last Modified: 2013-12-16
OS: Debian Sarge

Tomcat: Tomcat 5.0.28

Script in init.d:
#!/bin/sh
#
# chkconfig: 345 91 35
# description: Starts and TomCat servlet server
#              used to provide servlets servlet service for apache.

# See how we were called.

echo "This is the start of the tomcat script"


TOMCAT_HOME=/usr/local/Java/TomCat

case "$1" in

  start)

        for user in `ls $TOMCAT_HOME | awk '{ print $1; }'` ; do
            su ${user} -c "/usr/local/Scripts/tomcat.sh start"
        done

        ;;
  stop)
        for user in `ls $TOMCAT_HOME | awk '{ print $1; }'` ; do
            su ${user} -c "/usr/local/Scripts/tomcat.sh stop"
        done
        ;;
  restart)
        echo -n "Restarting TomCat server"
        echo ""
        $0 stop
        sleep 5
        $0 start
        ;;
*)
        echo "Usage: tomcat.sh {start|stop|restart}"
        exit 1
esac


Script in /usr/local/Scripts:

#!/bin/sh
#
# chkconfig: 345 91 35
# description: Starts and TomCat servlet server
#              used to provide servlets servlet service for apache.
JAVA_OPTS="-Xincgc -Xms128m -Xmx1024m"
# See how we were called.
TOMCAT_HOME=/usr/local/Java/TomCat

case "$1" in
  start)
        $0 kill
        cd /home/${USER}/Source/WEB-INF/classes
        /usr/local/Scripts/setCLASSPATH.sh "${TOMCAT_HOME}/${USER}/bin/catalina.sh start" > ${TOMCAT_HOME}/${USER}/logs/console.log 2>&1

        ;;
  stop)
        /usr/local/Scripts/setCLASSPATH.sh "${TOMCAT_HOME}/${USER}/bin/catalina.sh stop" > /dev/null

        ;;
  kill)
        kill -9 $(ps U $USER | grep "TomCat" | grep -v "tail" | awk '{print $1}') 2>&1 |cat> /dev/null
        ;;
  restart)
        echo -n "Restarting Tomcat server"
        $0 stop
        sleep 5
        $0 start
        echo ""
        ;;
*)
        echo "Usage: tomcat.sh {start|stop|restart}"
        exit 1
esac




RC updates run:
update-rc.d /etc/init.d/tomcat start 97 2 3 4 5 . stop 97 0 1 6 .


Generel info:

I have a tomcat running for each user on the system, all named after the username and placed in /usr/local/Java/TomCat/*
If I execute the above script manually, all works fine, but with the above given RC update tomcat does not start auto upon boot time.

I have a weird feeling that it is related to the "su" usage in the script, but I am not sure

Input on this subject is very much appreciated =)

The script works fine on Redhat/fedora startup
Comment
Watch Question

Top Expert 2006

Commented:
>I have a tomcat running for each user on the system

Looks like they are all usign the same configuration file, in which case they all trying to use the same port? As a result, the first will start, and all subsequent ones will fail.

HTH:)
Top Expert 2006

Commented:
Also there is a fairly high chance (depending on permissions, that the $user concerned will not be able to access the required files (log/configuration) etc.

...and if you expect to have more than 3 or 4 users running tomcat at once, I hope that you have a *very* powerful pc!

HTH:)
Top Expert 2006

Commented:
If you'd like to see exactly what the script is doing, then just before the su line you could add the line:

echo "At point 1" >> /tmp/mylog
echo ${USER} >> /tmp/mylog

in the startup script. Then check whether the appropriate user exists *WITH ACCESS* to the directory concerned.

HTH:)

Author

Commented:
Thanks for the input, but as I stated, everything works fine when the script is executed manually

Port configuration is in the server.xml file, one for each user, and not running on the same port =)
Log files etc. are located at the tomcat specific for and owned by the user.

I have very powerful servers =) IBM system P

But again thx for your thoughts and ideas
Top Expert 2006

Commented:
echo "At point 1" >> /tmp/mylog
echo ${USER} >> /tmp/mylog

And the output from these lines when run in the script is? What command line does that give you, and can you type tha line in? (Add as many of these as necessary in order to determine how far through the scripts it gets.)

I suspect that this will highlight the error?...as if the command line is identical, with the same user permissions then it will work.

The only additional thought is that due to the environement at this stage during boot up, it may be necessary to run the command with an & at the end. This may be required in order to make it run in the backgroung rather than 'hang' the script.

Author

Commented:
Hmm, seems like the system doesn't rekognize the $USER variable, the echo $USER comes back empty before su so no wonder it's unable to start up.

Do you have any thoughts how to deal with this, I thought the $USER variable was global from start
Top Expert 2006

Commented:
As suspected:)

Next problem:
----------------------8X-----------------
        for user in `ls $TOMCAT_HOME | awk '{ print $1; }'` ; do
            su ${user} -c "/usr/local/Scripts/tomcat.sh start"
        done
----------------------8X-----------------
In this you are setting ${user} to be the name of each of the directories that exist under $TOMCAT_HOME

Can you:

echo "At point 2" >> /tmp/mylog
echo $TOMCAT_HOME >> /tmp/mylog

from within the script before the bit quoted above. If this is wrong, then obviously the users won't be right either!

The problem I suspect is that $TOMCAT_HOME has not been initiated at this stage in the start up. Perhaps change this to a constant earlier in the script?

HTH:)

Author

Commented:
It is defined at the start of the script as a variable, I can make it a constant and see if that solves the problem.

But let me try to see what it prompts for TOMCAT_HOME, will be back asap

Author

Commented:
It is returning the correct path /usr/local/Java/TomCat =( so that's not it
Top Expert 2006

Commented:
If you run this from the command line, then TOMCAT_HOME should be correct, but at boot time, I'm surprised that TOMCAT_HOME is already defined.

Add:

echo `ls $TOMCAT_HOME` >> /tmp/mylog

before the first reference to TOMCAT_HOME

and check that it gives a list of the names that you want to start the process as.

What does it do now when startup occurs. How far does it get, and does it start up any tomcat servers?

I'm still concerned that you do not seem to have a seperate configuration file for eaxh user, as if a port is already in use, then trying to start another tomcat on the same port will fail.

HTH:)



Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I'm using jk for apache so it's not a port but a mountpoint on port 80 forwarded to the correct jk mount point via a frameset in apache for each site/tomcat

Author

Commented:
No need to be concerned =) I've been running this setup for 5 years now and it has worked like a charm. The reason I'm doing it is the classpath definitions on the code my company is producing, longer story =)

Author

Commented:
echo `ls $TOMCAT_HOME` >> /tmp/mylog before the variable statement TOMCAT_HOME=/usr/local/Java/TomCat gives me a listing of slash
echo `ls $TOMCAT_HOME` >> /tmp/mylog after variable gives me the correct tomcat dir listing, so that seems to be fine

"what does it do when startup occurs" I'm remoting the server so I can't see what is going on in the startup phase, this is on my list, but the particular server I'm working on is hosted elsewhere =)
"does it start up any tomcat servers" nope not a one.

"I'm still concerned that you do not seem to have a seperate configuration file for eaxh user" each user/tomcat has it's own config file jk have it's workers.properties where mount points are bind to port numbers and each user/tomcat have the server.xml file where port numbers for the specific tomcat are specified

Author

Commented:
*SIGH* Problem was that JAVA_HOME was not set as a global variable using su -c in the boot process, by setting JAVA_HOME in the tomcat RC script everything worked.

As an appreciation of your time helping me troubleshoot pjedmond KI'm gonna give you half the points for the question.

Author

Commented:
or all ;=)
Top Expert 2006

Commented:
Many thanks:)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.