Tomcat7 start at server startup

35,120

Solution 1

I've found the problem:

Another init.d script is blocking for several minutes the init.d/tomcat7 execution.

In my case I can see in my console this error repeated several times when startup:

2014 Jul  6 11:42:39 vps56629 NOQUEUE: SYSERR(root): opendaemonsocket: daemon MTA-v4: cannot bind: Address already in use
2014 Jul  6 11:42:39 vps56629 daemon MTA-v4: problem creating SMTP socket
2

When the error appears in console init.d/tomcat7 script starts execution.

I've disabled sendmail, but it wasn't the real prolem.

The blocking script is iptables rules running too slow because of server lookup operations, **I've just removed the following line in /etc/init.d/iptables and now is ok:**

/sbin/iptables -L

Solution 2

make sure that both of tomcat & apache aren't online

1st you have to make a link from /etc/rc5.d :

cd /etc/rc5.d
sudo ln -s ../init.d/tomcat S71tomcat
sudo ln -s ../init.d/apache S72apache

for the tomcat make a file & name it tomcat in this dir /etc/init.d/

sudo touch /etc/init.d/tomcat
gksu 'gedit /etc/init.d/tomcat'

past this

----------------------------  /etc/init.d/tomcat  ------------------------
#!/bin/bash
#
# tomcat        
#
# chkconfig: 
# description:  Start up the Tomcat servlet engine.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
CATALINA_HOME="/usr/apps/apache/tomcat/jakarta-tomcat-4.0.4"

case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
        echo $"Starting Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/startup.sh
        fi
    ;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
        echo $"Stopping Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
        fi
    ;;
 *)
    echo $"Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit $RETVAL
-----------------------  end of /etc/init.d/tomcat  ----------------------

for the apache $ sudo touch /etc/init.d/apache $ gksu 'gedit /etc/init.d/apache'

then past this

----------------------------  /etc/init.d/apache  ------------------------
#!/bin/bash
#
# apache        
#
# chkconfig: 
# description:  Start up the Apache web server.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
APACHE_HOME="/usr/apps/apache/apache"

case "$1" in
 start)
    if [ -f $APACHE_HOME/bin/apachectl ]; then
        echo $"Starting Apache"
        $APACHE_HOME/bin/apachectl start
    fi
    ;;
 stop)
    if [ -f $APACHE_HOME/bin/apachectl ]; then
        echo $"Stopping Apache"
        $APACHE_HOME/bin/apachectl stop
    fi
    ;;
 *)
    echo $"Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit $RETVAL
-----------------------  end of /etc/init.d/apache  ----------------------

then reboot

$ gksu shutdown -r
Share:
35,120
surfealokesea
Author by

surfealokesea

Updated on September 18, 2022

Comments

  • surfealokesea
    surfealokesea over 1 year

    My ubuntu 13.10 was configured to start apache2 and tomcat7 at startup, but (maybe for automatic system updates) they don't auto-start at startup anymore.

    I've solved apache2 auto start with the following commands:

    update-rc.d -f apache2 remove
    update-rc.d apache2 add
    update-rc.d apache2 enable
    

    But same ones for tomcat7 did not work! I searched at logs looking for errors but nothing found.

    THIS WORKS:

    service tomcat7 start
    

    my /etc/init.d/tomcat7 standard file:

        #!/bin/sh
        #
        # /etc/init.d/tomcat7 -- startup script for the Tomcat 6 servlet engine
        #
        # Written by Miquel van Smoorenburg <[email protected]>.
        # Modified for Debian GNU/Linux by Ian Murdock <[email protected]>.
        # Modified for Tomcat by Stefan Gybas <[email protected]>.
        # Modified for Tomcat6 by Thierry Carrez <[email protected]>.
        # Modified for Tomcat7 by Ernesto Hernandez-Novich <[email protected]>.
        # Additional improvements by Jason Brittain <[email protected]>.
        #
        ### BEGIN INIT INFO
        # Provides:          tomcat7
        # Required-Start:    $local_fs $remote_fs $network
        # Required-Stop:     $local_fs $remote_fs $network
        # Should-Start:      $named
        # Should-Stop:       $named
        # Default-Start:     2 3 4 5
        # Default-Stop:      0 1 6
        # Short-Description: Start Tomcat.
        # Description:       Start the Tomcat servlet engine.
        ### END INIT INFO
    
        set -e
    
    
    echo "============================================================================" >> /var/log/tomcat7/catalina.out
    echo "$(date) /etc/init.d/tomcat -> starting/stoping/restarting" >> /var/log/tomcat7/catalina.out
    echo "============================================================================" >> /var/log/tomcat7/catalina.out
    
    
        PATH=/bin:/usr/bin:/sbin:/usr/sbin
        NAME=tomcat7
        DESC="Tomcat servlet engine"
        DEFAULT=/etc/default/$NAME
        JVM_TMP=/tmp/tomcat7-$NAME-tmp
    
        if [ `id -u` -ne 0 ]; then
                echo "You need root privileges to run this script"
                exit 1
        fi
    
        # Make sure tomcat is started with system locale
        if [ -r /etc/default/locale ]; then
                . /etc/default/locale
                export LANG
        fi
    
        . /lib/lsb/init-functions
    
        if [ -r /etc/default/rcS ]; then
                . /etc/default/rcS
        fi
    
    
        # The following variables can be overwritten in $DEFAULT
    
        # Run Tomcat 7 as this user ID and group ID
        TOMCAT7_USER=tomcat7
        TOMCAT7_GROUP=tomcat7
        # this is a work-around until there is a suitable runtime replacement
        # for dpkg-architecture for arch:all packages
        # this function sets the variable OPENJDKS
        find_openjdks()
        {
                for jvmdir in /usr/lib/jvm/java-7-openjdk-*
                do
                        if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-7-openjdk-common" ]
                        then
                                OPENJDKS=$jvmdir
                        fi
                done
                for jvmdir in /usr/lib/jvm/java-6-openjdk-*
                do
                        if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-6-openjdk-common" ]
                        then
                                OPENJDKS="${OPENJDKS} ${jvmdir}"
                        fi
                done
        }
    
        OPENJDKS=""
        find_openjdks
        # The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
        # defined in $DEFAULT)
        JDK_DIRS="/usr/lib/jvm/default-java ${OPENJDKS} /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-7-oracle"
    
        # Look for the right JVM to use
        for jdir in $JDK_DIRS; do
            if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
                JAVA_HOME="$jdir"
            fi
        done
        export JAVA_HOME
    
        # Directory where the Tomcat 6 binary distribution resides
        CATALINA_HOME=/usr/share/$NAME
    
        # Directory for per-instance configuration files and webapps
        CATALINA_BASE=/var/lib/$NAME
        # Use the Java security manager? (yes/no)
        TOMCAT7_SECURITY=no
    
        # Default Java options
        # Set java.awt.headless=true if JAVA_OPTS is not set so the
        # Xalan XSL transformer can work without X11 display on JDK 1.4+
        # It also looks like the default heap size of 64M is not enough for most cases
        # so the maximum heap size is set to 128M
        if [ -z "$JAVA_OPTS" ]; then
                JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
        fi
    
        # End of variables that can be overwritten in $DEFAULT
        # overwrite settings from default file
        if [ -f "$DEFAULT" ]; then
                . "$DEFAULT"
        fi
    
        if [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; then
                log_failure_msg "$NAME is not installed"
                exit 1
        fi
    
        POLICY_CACHE="$CATALINA_BASE/work/catalina.policy"
    
        if [ -z "$CATALINA_TMPDIR" ]; then
                CATALINA_TMPDIR="$JVM_TMP"
        fi
    
        # Set the JSP compiler if set in the tomcat7.default file
        if [ -n "$JSP_COMPILER" ]; then
                JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\""
        fi
    
        SECURITY=""
        if [ "$TOMCAT7_SECURITY" = "yes" ]; then
                SECURITY="-security"
        fi
    
        # Define other required variables
        CATALINA_PID="/var/run/$NAME.pid"
        CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"
    
        # Look for Java Secure Sockets Extension (JSSE) JARs
        if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
            JSSE_HOME="${JAVA_HOME}/jre/"
        fi
        catalina_sh() {
                # Escape any double quotes in the value of JAVA_OPTS
                JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')"
    
                AUTHBIND_COMMAND=""
                if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
                        JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
                        AUTHBIND_COMMAND="/usr/bin/authbind --deep /bin/bash -c "
                fi
    
                # Define the command to run Tomcat's catalina.sh as a daemon
                # set -a tells sh to export assigned variables to spawned shells.
                TOMCAT_SH="set -a; JAVA_HOME=\"$JAVA_HOME\"; source \"$DEFAULT\"; \
                        CATALINA_HOME=\"$CATALINA_HOME\"; \
                        CATALINA_BASE=\"$CATALINA_BASE\"; \
                        JAVA_OPTS=\"$JAVA_OPTS\"; \
                        CATALINA_PID=\"$CATALINA_PID\"; \
                        CATALINA_TMPDIR=\"$CATALINA_TMPDIR\"; \
                        LANG=\"$LANG\"; JSSE_HOME=\"$JSSE_HOME\"; \
                        cd \"$CATALINA_BASE\"; \
                        \"$CATALINA_SH\" $@"
    
                if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
                        TOMCAT_SH="'$TOMCAT_SH'"
                fi
    
                # Run the catalina.sh script as a daemon
                set +e
                touch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
                chown $TOMCAT7_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
                start-stop-daemon --start -b -u "$TOMCAT7_USER" -g "$TOMCAT7_GROUP" \
                        -c "$TOMCAT7_USER" -d "$CATALINA_TMPDIR" -p "$CATALINA_PID" \
                        -x /bin/bash -- -c "$AUTHBIND_COMMAND $TOMCAT_SH"
                status="$?"
                set +a -e
                return $status
        }
    
        case "$1" in
          start)
                if [ -z "$JAVA_HOME" ]; then
                        log_failure_msg "no JDK or JRE found - please set JAVA_HOME"
                        exit 1
                fi
    
                if [ ! -d "$CATALINA_BASE/conf" ]; then
                        log_failure_msg "invalid CATALINA_BASE: $CATALINA_BASE"
                        exit 1
                fi
    
                log_daemon_msg "Starting $DESC" "$NAME"
                if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                        --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
                        >/dev/null; then
    
                        # Regenerate POLICY_CACHE file
                        umask 022
                        echo "// AUTO-GENERATED FILE from /etc/tomcat7/policy.d/" \
                                > "$POLICY_CACHE"
                        echo ""  >> "$POLICY_CACHE"
                        cat $CATALINA_BASE/conf/policy.d/*.policy \
                                >> "$POLICY_CACHE"
    
                        # Remove / recreate JVM_TMP directory
                        rm -rf "$JVM_TMP"
                        mkdir -p "$JVM_TMP" || {
                                log_failure_msg "could not create JVM temporary directory"
                                exit 1
                        }
                        chown $TOMCAT7_USER "$JVM_TMP"
    
                        catalina_sh start $SECURITY
                        sleep 5
                        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                                --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
                                >/dev/null; then
                                if [ -f "$CATALINA_PID" ]; then
                                        rm -f "$CATALINA_PID"
                                fi
                                log_end_msg 1
                        else
                                log_end_msg 0
                        fi
                else
                        log_progress_msg "(already running)"
                        log_end_msg 0
                fi
                ;;
          stop)
                log_daemon_msg "Stopping $DESC" "$NAME"
    
                set +e
                if [ -f "$CATALINA_PID" ]; then
                        start-stop-daemon --stop --pidfile "$CATALINA_PID" \
                                --user "$TOMCAT7_USER" \
                                --retry=TERM/20/KILL/5 >/dev/null
                        if [ $? -eq 1 ]; then
                                log_progress_msg "$DESC is not running but pid file exists, cleaning up"
                        elif [ $? -eq 3 ]; then
                                PID="`cat $CATALINA_PID`"
                                log_failure_msg "Failed to stop $NAME (pid $PID)"
                                exit 1
                        fi
                        rm -f "$CATALINA_PID"
                        rm -rf "$JVM_TMP"
                else
                        log_progress_msg "(not running)"
                fi
                log_end_msg 0
                set -e
                ;;
           status)
                set +e
                start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                        --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
                        >/dev/null 2>&1
                if [ "$?" = "0" ]; then
    
                        if [ -f "$CATALINA_PID" ]; then
                            log_success_msg "$DESC is not running, but pid file exists."
                                exit 1
                        else
                            log_success_msg "$DESC is not running."
                                exit 3
                        fi
                else
                                log_progress_msg "$DESC is not running but pid file exists, cleaning up"
                        elif [ $? -eq 3 ]; then
                                PID="`cat $CATALINA_PID`"
                                log_failure_msg "Failed to stop $NAME (pid $PID)"
                                exit 1
                        fi
                        rm -f "$CATALINA_PID"
                        rm -rf "$JVM_TMP"
                else
                        log_progress_msg "(not running)"
                fi
                log_end_msg 0
                set -e
                ;;
           status)
                set +e
                start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                        --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
                        >/dev/null 2>&1
                if [ "$?" = "0" ]; then
    
                        if [ -f "$CATALINA_PID" ]; then
                            log_success_msg "$DESC is not running, but pid file exists."
                                exit 1
                        else
                            log_success_msg "$DESC is not running."
                                exit 3
                        fi
                else
                        log_success_msg "$DESC is running with pid `cat $CATALINA_PID`"
                fi
                set -e
                ;;
          restart|force-reload)
                if [ -f "$CATALINA_PID" ]; then
                        $0 stop
                        sleep 1
                fi
                $0 start
                ;;
          try-restart)
                if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                        --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
                        >/dev/null; then
                        $0 start
                fi
                ;;
          *)
                log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
                exit 1
                ;;
        esac
    
        exit 0
    

    Any help would be appreciate, Thanks

    • hwez
      hwez almost 10 years
    • surfealokesea
      surfealokesea almost 10 years
      my /etc/init.d/tomcat7 is more complex than the one in this url, anyway I've followed the steps described & still not working. And if I try to start it manually with the script defined in 223944 my tomcat is not working properly
    • hwez
      hwez almost 10 years
      can you reinstal it ?
    • surfealokesea
      surfealokesea almost 10 years
      It would be an option but I'd prefer to fix it first if possible
  • surfealokesea
    surfealokesea almost 10 years
    thanks 4 your reply, my service is called tomcat7, I've allready a lrwxrwxrwx 1 root root 17 Jul 5 09:53 S20tomcat7 -> ../init.d/tomcat7* at /etc/rc5.d, why should I change my init.d/tomcat7 for your version when my version is the stardard and I didn't change it?
  • surfealokesea
    surfealokesea almost 10 years
    your init.d/tomcat script is not valid for my tomcat7, tomcat7 has another version that works well calling, for example, service tomcat stop|start|restart.
  • hwez
    hwez almost 10 years
    you have to change this CATALINA_HOME= its just an example
  • surfealokesea
    surfealokesea almost 10 years
    yes, I have noticed, and did it. But when I run with this script my tomcat is not running well (misconfigured) neither starting at startup. Have a look at my /etc/init.d/tomcat7 standard file, this one is the standard that ubuntu installs (gist.github.com/baylisscg/942150). I've also noticed the rc5.d entries, in my case where S20apache2 & S20tomcat7 and I've changed to S21tomcat7 & S22apache, the result is the same at startup: apache starts, tomcat doesn't start
  • hwez
    hwez almost 10 years
    Try to 're install
  • Dejel
    Dejel about 8 years
    why do we need the softlink? (1st you have to make a link from /etc/rc5.d : )