martes, julio 06, 2010

Iniciar Automaticamente Tomcat en Centos

Autoarrancar Tomcat
1. Crear un script en /etc/init.d para automatizar tomcat
# cd /etc/init.d
# vi tomcat

2. Digite lo siguiente:
#!/bin/bash
#
# Iniciación automatica de TOMCAT6D
#
# chkconfig: 2345 55 25
# description: Demonio de iniciación TOMCAT6D 6.0.29
#
# processname: tomcat6d
# pidfile: /var/run/tomcat6d.pid

export JAVA_HOME=/usr/java/jdk1.6.0_20
export CATALINA_HOME=/usr/local/tomcat
start(){
       echo "Iniciando Tomcat6"
       $CATALINA_HOME/bin/startup.sh
       }

stop() {
       echo "Deteniendo Tomcat6"
       $CATALINA_HOME/bin/shutdown.sh
       }
restart(){
       stop
       start
         }

# See how we were called.
case $1 in
     start)
        start
;;
     stop)
        stop
;;
     restart)
        restart
;;
     *)
echo "Utilice los Parámetros: $0 {start|stop|restart}"
     exit 1
esac
     exit 0


2. Cambiar los permisos, para que se pueda ejecutar


# chmod a+x /etc/init.d/tomcat

3. Agregar este script to servicios de sistema
# chkconfig --add tomcat 
4. Verificar modficaciones (este script utiliza Niveles 2,3 y 4)
# chkconfig --level 234 tomcat on
# chkconfig --list tomcat


5.- Agregamos este scrip como un link simbolico para que se inicie automaticamente.
#ln -s /etc/init.d/tomcat /etc/rc5.d/S71tomcat

El 71 es un numero que uno elige segun para que sepa despues o antes de que proceso quiere que se inicie, logicamente debe ser despues que se haya iniciado mysql echen una mirada a al /etc/init.d
Revisar en la lista que este servicio este utilizando estos Niveles 2, 3 y 4:
tomcat 0:off 1:off 2:on 3:on 4:on 5:off 6:off
6. Probar el script con start/stop
# service tomcat start
# service tomcat stop
A este punto el servicio tomcat se ejecutará automáticamente al iniciar el servidor.

Aqui existe un documento con los scripts para descargar:
Scripts de Tomcat automatico

Script : tomcatd

#!/bin/bash
#
# Startup script for Jakarta Tomcat
# Script should work on Centos, Redhat and Fedora Linux. It depends on the file tomcatRunner.
# WARNING: The script does not allow to run Tomcat on privileged ports as non root user.
# For this use case try : http://tomcat.apache.org/tomcat-6.0-doc/setup.html and http://commons.apache.org/daemon/jsvc.html
#
# Should start normally after the databases and before http server
# chkconfig: 345 80 10
# description: Jakarta Tomcat Java Servlet/JSP Container
# processname: tomcatd
# pidfile: /var/run/tomcat/tomcatd.pid
# source function library
. /etc/rc.d/init.d/functions

##### In this area you can find settings which are likely to change frequently ####

JAVA=/usr/java/jdk1.6.0_07/jre/bin/java
# unprivileged user running Tomcat server
tomcatuser=tomcat

# servicename used as pidfile and lockfile name, must correspond to 'processname:' at the top of this file
# If not linux will not detect the running service during runlevel switch and will not shut it down normally
servicename=tomcatd

# folder where Tomcat is installed
CATALINA_HOME=/opt/tomcat
# Options for the JVM
JAVA_OPTS=-Xmx256m
#JAVA_OPTS=-Xmx512m -XX:MaxPermSize=128m
##### End of frequent settings area #####

#script used to startup tomcat
tomcatRunner=/etc/init.d/tomcatRunner
pidfile=/var/run/tomcat/$servicename
lockfile=/var/lock/subsys/$servicename
#runsecure=1 #starts tomcat with java security
runsecure=0

# Optional additional libs you would like to add to the classpath (= JVM Option -classpath)
CLASSPATH=""
# Optional Java Security Socket extension
# CLASSPATH="$CLASSPATH":"$JSSE_HOME"/lib/jcert.jar:"$JSSE_HOME"/lib/jnet.jar:"$JSSE_HOME"/lib/jsse.jar

# path to Tomcat lib
CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/bootstrap.jar

# Directory holding configuration, defaults to CATALINA_HOME
# In a Tomcat cluster you might reuse the servicename to identify the base directory

CATALINA_BASE="$CATALINA_HOME"
# server log during startup / shutdown
logfile=$CATALINA_BASE/logs/catalina.out
# endorsed allows to overwrite JVM libs -> JVM option -Djava.endorsed.dirs
#JAVA_ENDORSED_DIRS="$CATALINABASEDIR"/endorsed

# Define the java.io.tmpdir to use for Catalina
CATALINA_TMPDIR="$CATALINA_BASE"/temp

# Set juli LogManager if it is present
if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then
  JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
  LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
fi

#### End of settings #####

# build java command to start Tomcat
command="$JAVA $JAVA_OPTS $LOGGING_CONFIG $CATALINA_OPTS $LOGGING_CONFIG \
      -Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS -classpath $CLASSPATH \
      -Dcatalina.base=$CATALINA_BASE \
      -Dcatalina.home=$CATALINA_HOME \
      -Djava.io.tmpdir=$CATALINA_TMPDIR"
     
if [ "$runsecure" = "1" ]; then
  command="$command -Djava.security.manager -Djava.security.policy=$CATALINA_BASE/conf/catalina.policy"
fi

command="$tomcatRunner $pidfile $logfile $command org.apache.catalina.startup.Bootstrap"     
     

start()
{
    echo $"Starting $servicename based at $CATALINA_BASE "

    daemon --user $tomcatuser --pidfile=$pidfile $command start
    RETVAL=$?
   
    [ "$RETVAL" = 0 ] && touch $lockfile
    echo
}

stop()
{
    echo -n $"Stopping $prog: "
    if [ ! -r $pidfile ]; then
        echo "Pidfile $pidfile cannot be read"
        RETVAL=1
        return
    fi
    # Sends TERM signal first and kills finally after 3 seconds
    killproc -p $pidfile -d 3 $servicename
# Send TERM signal only, don't kill
#    killproc -p $pidfile $servicename -15
    RETVAL=$?
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
    echo

}

version()
{
    $JAVA -classpath $CATALINA_HOME/lib/catalina.jar org.apache.catalina.util.ServerInfo
    RETVAL=$?
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    version)
        version
        ;;
    status)
        status -p $pidfile $servicename
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|version|status}"
        RETVAL=1
esac
exit $RETVAL

Script: tomcatrunner



#!/bin/bash
# Sebastian Hennebrueder
# http://www.laliluna.de
# Executes a Tomcat startup command and notes the PID
# parm 1 is the pidfile
# param 2 is the tomcat logfile to which the output is redirected
# param 3 is the command
# param 4 .. n will be added to the command

if [ $# -lt 3 ]; then
  echo "Usage: $0 pidfile logfile command"
  exit 1
fi

# extract params
pidfile="$1"
logfile="$2"
cmd="$3"
shift 3

# check if we can access pidfile
touch "$pidfile"

if [ ! $? = "0" ]; then
    echo "Could not touch pidfile: $pidfile"
    exit 1;
fi


# run command
$cmd $@ >> "$logfile"  2>&1 &
pid=$!

# check that the process is running

if [ ! -d "/proc/$pid" ]; then
    echo "Error starting Tomcat, check the logfile at $logfile"
    exit 1;
fi

# write pid file
echo "$pid" > "$pidfile" 

if [ ! $? = "0" ]; then
    echo "Could not write pid to pidfile: $pidfile"
    exit 1;
fi
exit 0




No hay comentarios: