Мой сценарий запуска tomcat не запускает tomcat при перезагрузке сервера Red Hat Enterprise Linux.
Я сузил его до функции запуска:
41 start() {
42 echo "instart $(date)" > /tmp/tomcatscript.out
43 pid=$(tomcat_pid)
44 if [ -n "$pid" ]
45 then
46 echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
47 else
48 echo "inelse $(date)" >> /tmp/tomcatscript.out
49 # Start tomcat
50 echo -e "\e[00;32mStarting tomcat\e[00m"
51 #ulimit -n 100000
52 #umask 007
53 #/bin/su -p -s /bin/sh $TOMCAT_USER
54 if [ `user_exists $TOMCAT_USER` = "1" ]
55 then
56 echo "in if then PID [$pid] whoami [$(whoami)] $(date)">> /tmp/tomcatscript.out
57 sudo su - $TOMCAT_USER -s /bin/sh -c $CATALINA_HOME/bin/startup.sh >> /tmp/tomcatscript.out
58 else
59 echo "in else $(date)" >> /tmp/tomcatscript.out
60 echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
61 sh $CATALINA_HOME/bin/startup.sh
62 fi
63 echo "calling status $(date)">> /tmp/tomcatscript.out
64
65 status >> /tmp/tomcatscript.out
66 fi
67 return 0
68 }
Когда я перезагружаю сервер, на котором запущен / sbin / reboot, содержимое файла, который я пересылаю, будет:
# cat /tmp/tomcatscript.out
instart Wed Jun 15 20:24:25 PDT 2016
inelse Wed Jun 15 20:24:25 PDT 2016
in if then PID [] whoami [root] Wed Jun 15 20:24:25 PDT 2016
calling status Wed Jun 15 20:24:25 PDT 2016
Tomcat is not running
Когда я запускаю сценарий tomcat в /etc/rc.d/init.d следующим образом:
[root@server init.d]# ./tomcat start
Содержимое файла:
[root@server init.d]# cat /tmp/tomcatscript.out
instart Wed Jun 15 20:38:30 PDT 2016
inelse Wed Jun 15 20:38:30 PDT 2016
in if then PID [] whoami [root] Wed Jun 15 20:38:30 PDT 2016
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.
calling status Wed Jun 15 20:38:30 PDT 2016
Tomcat is not running
* Я также пробовал использовать функцию демона - у меня это тоже не сработало *
#!/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
}
Теперь он работает, когда я меняю 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