Runing Tomcat as Linux service

Not big deal but I am bit tired to google for this script each time I setup new box. Now I know where to get it without spending two valuable extra minutes to google. First create /etc/rc.d/init.d/tomcatd :

#!/bin/bash  
#  
# Startup script for Tomcat  
#  
# chkconfig: 345 84 16  
# description: Tomcat jakarta JSP server  
TOMCAT_HOME=/usr/share/apache-tomcat-5.5.20  
TOMCAT_START=$TOMCAT_HOME/bin/startup.sh  
TOMCAT_STOP=$TOMCAT_HOME/bin/shutdown.sh  
#Necessary environment variables  
export JAVA_HOME=”/usr/share/jdk1.5.0_10?  
export CATALINA_HOME=”/usr/share/apache-tomcat-5.5.20?  
#export LD_KERNEL_ASSUME=”2.2.5?  
# Source function library.  
. /etc/rc.d/init.d/functions  
# Source networking configuration.  
. /etc/sysconfig/network  
# Check that networking is up.  
[ ${NETWORKING} = “no” ] && exit 0  
#Check for tomcat script  
if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]  
then  
    echo “Tomcat not available…”  
    exit  
fi  
start() {  
    echo -n “Starting Tomcat: “  
    su - tomcat -c $TOMCAT_START  
    echo  
    touch /var/lock/subsys/tomcatd  
# We may need to sleep here so it will be up for apache  
    sleep 3  
#Instead should check to see if apache is up by looking for httpd.pid  
}  
stop() {  
    echo -n $”Shutting down Tomcat: “  
    su - tomcat -c $TOMCAT_STOP  
    rm -f /var/lock/subsys/tomcatd  
    echo  
}  
status() {  
    ps ax –width=1000 | grep “[o]rg.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 ” “}’ | wc | awk ‘{print $2}’ > /tmp/tomcat_process_count.txt  
   read line

Add user tomcat. Please note that it is normal user with his own home. Many times it was very useful.

adduser tomcat

Set JAVA_HOME for all users in /etc/profile.d/java.sh . When we run ’su – tomcat’ this script will set up our environment:

export JAVA_HOME=/usr/share/jdk1.5.0_10

Give enough permissions to tomacat:

chown tomcat /usr/share/apache-tomcat-5.5.20/ -R

Now you are able to run tomcat with

service tomcatd start

The last thing is to run it when system boots:

chkconfig –add tomcatd  
chkconfig tomcatd on

Done

16 Responses to “Runing Tomcat as Linux service”

  1. srk & rkini Says:

    you forgot to add this 🙂

    case “$1″ in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    start
    ;;
    reload)
    reload
    ;;
    status)
    status
    RETVAL=$?
    ;;
    *)
    echo $”Usage: $0 {start|stop|restart|reload|status}”
    RETVAL=1
    esac

  2. javarules Says:

    And for beginning shell programmers watch the use of “ and ” that gave me an error until I cnverted all the “ to “(tx to vim)

    I also changed these lines
    export JAVA_HOME=”/usr/share/jdk1.5.0_10?
    export CATALINA_HOME=”/usr/share/apache-tomcat-5.5.20?

    to these
    export JAVA_HOME=/usr/share/jdk1.5.0_10
    export CATALINA_HOME=/usr/share/apache-tomcat-5.5.20

    tx anyway ciao

  3. Ali Says:

    I start my tomcat on my ssh and it works properly but when I close my ssh session it doesn’t work any more.

    I have debian so I don’t have :
    chkconfig –add tomcatd
    chkconfig tomcatd on

    how can I solve my problem?

    thank you.

    Ali

  4. Chris Gralike Says:

    Maybe this script might help you out.

    http://sysengineers.wordpress.com/2010/08/10/rhel5-init-script-for-tomcat-catalina/

    It utilizes the linux subsystem and is chkconfig enabled. It only requires you to tweak one path (pointing to catalina) to make it work.

  5. New Orleans advertising Says:

    New Orleans advertising agencies…

    […]Runing Tomcat as Linux service « auJava[…]…

  6. Prestashop Themes Says:

    Prestashop Themes…

    […]Runing Tomcat as Linux service « auJava[…]…

  7. satellite TV info Says:

    satellite TV info…

    […]Runing Tomcat as Linux service « auJava[…]…

  8. Empresa de criação de sites, aplicativos web, lojas virtuais, manutenção de sites, otimização de site e muitos outros serviços web. A empresa foi constituída para atender desde o pequeno e médio empreendedor como as grandes empresas, ONG, Institu Says:

    Empresa de criação de sites, aplicativos web, lojas virtuais, manutenção de sites, otimização de site e muitos outros serviços web. A empresa foi constituída para atender desde o pequeno e médio empreendedor como as grandes empresas, ONG, Instituiçõe…

    […]Runing Tomcat as Linux service « auJava[…]…

  9. fashion jewelry wholesale Says:

    fashion jewelry wholesale…

    […]Runing Tomcat as Linux service « auJava[…]…

  10. MySQL,PHP Says:

    MySQL,PHP…

    […]Runing Tomcat as Linux service « auJava[…]…

  11. Women Info Says:

    It’s very straightforward to find out any matter on web as compared to textbooks, as I found this article at this site.

  12. Slim Berry Max Review Says:

    Its like you read my mind! You seem to grasp a lot about this, such as
    you wrote the e book in it or something. I feel that you just can do with a few % to drive the message home a bit, however other than that, that is great blog. A fantastic read. I’ll certainly be back.

  13. Free Porn Tube Says:

    Hi there, every time i used to check website posts here early in the morning, because i like to gain knowledge
    of more and more.

  14. Dat NSFW Says:

    Hey There. I found your blog using msn. This is an extremely well written article.

    I will make sure to bookmark it and come back to read
    more of your useful info. Thanks for the post. I will definitely comeback.

  15. advertising jakarta Says:

    Appreciation to my father who told me on the topic of this blog, this website is genuinely awesome.

  16. Abusive Relationship Says:

    Abusive Relationship

    Runing Tomcat as Linux service | auJava

Leave a reply to Chris Gralike Cancel reply