1

У меня есть следующий скрипт запуска, который при перезагрузке не вызывает tomcat:

#!/bin/bash
#
# description: Apache Tomcat init script
# processname: tomcat
# chkconfig: 234 20 80
#
### BEGIN INIT INFO
# Provides:        tomcat8
# Required-Start:  2 3 4 5
# Required-Stop:   0 1 6
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO

#Location of JAVA_HOME (bin files)
export JAVA_HOME=/user/java/jdk1.8.0_71

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/users/tomcat/apache-tomcat-8.0.30

#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/users/tomcat/apache-tomcat-8.0.30/conf

#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=tomcat

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}

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

start() {
echo "instart $(date)" > /tmp/tomcatscript.out
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
echo "inelse $(date)" >> /tmp/tomcatscript.out
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
#    ulimit -n 100000
 #   umask 007
  #  /bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                echo "in if then PID [$pid] whoami [$(whoami)] $(date)">> /tmp/tomcatscript.out
                echo "[$TOMCAT_USER] and [$CATALINA_HOME]" >> /tmp/tomcatscript.out
                daemon --user $TOMCAT_USER  $CATALINA_HOME/bin/startup.sh > /dev/null
#                sudo su - $TOMCAT_USER -s /bin/sh -c $CATALINA_HOME/bin/startup.sh >> /tmp/tomcatscript.out
                echo "called daemon" >>  /tmp/tomcatscript.out
        else
                echo "in else $(date)" >> /tmp/tomcatscript.out
                echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh
        fi
        echo "calling status $(date)">> /tmp/tomcatscript.out

       status >> /tmp/tomcatscript.out
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]
            then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else
            echo -e "\e[00;31mTomcat is not running\e[00m"
            return 3
          fi
}

terminate() {
        echo -e "\e[00;31mTerminating Tomcat\e[00m"
        kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
               sudo su - $TOMCAT_USER -s /bin/sh -c $CATALINA_HOME/bin/shutdown.sh
    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}

case $1 in
        start)
          start
        ;;
        stop)
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
                status
                exit $?
        ;;
        kill)
                terminate
        ;;
        *)
                echo -e $TOMCAT_USAGE
        ;;
esac

exit 0

Выходные данные операторов echo:

[root@server init.d]# cat /tmp/tomcatscript.out
instart Thu Jun 23 15:40:03 PDT 2016
inelse Thu Jun 23 15:40:03 PDT 2016
in if then PID [] whoami [root] Thu Jun 23 15:40:03 PDT 2016
[tomcat] and [/users/tomcat/apache-tomcat-8.0.30]
Using CATALINA_BASE:   /users/tomcat/apache-tomcat-8.0.30/conf
Using CATALINA_HOME:   /users/tomcat/apache-tomcat-8.0.30
Using CATALINA_TMPDIR: /users/tomcat/apache-tomcat-8.0.30/conf/temp
Using JRE_HOME:        /users/java/jdk1.8.0_71
Using CLASSPATH:       /users/tomcat/apache-tomcat-8.0.30/bin/bootstrap.jar:/users/tomcat/apache-tomcat-8.0.30/bin/tomcat-juli.jar

Интересно, что если я запускаю его из терминала от имени пользователя root, обратите внимание, что CATALINA_BASE - это другой каталог:

[root@server init.d]# . /etc/init.d/functions
[root@server init.d]#     daemon --user tomcat /users/tomcat/apache-tomcat-8.0.30/bin/startup.sh
Using CATALINA_BASE:   /users/tomcat/apache-tomcat-8.0.30
Using CATALINA_HOME:   /users/tomcat/apache-tomcat-8.0.30
Using CATALINA_TMPDIR: /users/tomcat/apache-tomcat-8.0.30/temp
Using JRE_HOME:        /users/java/jdk1.8.0_71
Using CLASSPATH:       /users/tomcat/apache-tomcat-8.0.30/bin/bootstrap.jar:/users/tomcat/apache-tomcat-8.0.30/bin/tomcat-juli.jar
Tomcat started.
[root@server init.d]#                            [  OK  ]
called daemon                                              [FAILED]
calling status Thu Jun 23 15:40:03 PDT 2016
Tomcat is not running

1 ответ1

1

Теперь он работает, когда я изменяю CATALINA_HOME и CATALINA_BASE на:

#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/users/tomcat/apache-tomcat-8.0.30

#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/users/tomcat/apache-tomcat-8.0.30

Таким образом, весь скрипт, который работает для меня, выглядит так:

#!/bin/bash
#
# description: Apache Tomcat init script
# processname: tomcat
# chkconfig: 234 20 80
#
### BEGIN INIT INFO
# Provides:        tomcat8
# Required-Start:  2 3 4 5
# Required-Stop:   0 1 6
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO

#Location of JAVA_HOME (bin files)
export JAVA_HOME=/user/java/jdk1.8.0_71

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/users/tomcat/apache-tomcat-8.0.30

#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/users/tomcat/apache-tomcat-8.0.30

#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=tomcat

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}

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

start() {
echo "instart $(date)" > /tmp/tomcatscript.out
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
echo "inelse $(date)" >> /tmp/tomcatscript.out
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
#    ulimit -n 100000
 #   umask 007
  #  /bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                echo "in if then PID [$pid] whoami [$(whoami)] $(date)">> /tmp/tomcatscript.out
                echo "[$TOMCAT_USER] and [$CATALINA_HOME]" >> /tmp/tomcatscript.out
                daemon --user $TOMCAT_USER  $CATALINA_HOME/bin/startup.sh >> /tmp/tomcatscript.out
#                sudo su - $TOMCAT_USER -s /bin/sh -c $CATALINA_HOME/bin/startup.sh >> /tmp/tomcatscript.out
                echo "called daemon" >>  /tmp/tomcatscript.out
        else
                echo "in else $(date)" >> /tmp/tomcatscript.out
                echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh
        fi
        echo "calling status $(date)">> /tmp/tomcatscript.out

       status >> /tmp/tomcatscript.out
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]
            then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else
            echo -e "\e[00;31mTomcat is not running\e[00m"
            return 3
          fi
}

terminate() {
        echo -e "\e[00;31mTerminating Tomcat\e[00m"
        kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
               sudo su - $TOMCAT_USER -s /bin/sh -c $CATALINA_HOME/bin/shutdown.sh
    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}

case $1 in
        start)
          start
        ;;
        stop)
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
                status
                exit $?
        ;;
        kill)
                terminate
        ;;
        *)
                echo -e $TOMCAT_USAGE
        ;;
esac

exit 0

Всё ещё ищете ответ? Посмотрите другие вопросы с метками .