我的 tomcat 启动脚本没有在 Red Hat Enterprise Linux 服务器重新启动时启动 tomcat。
我已将其范围缩小到启动功能:
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
当我运行 /etc/rc.d/init.d 中的 tomcat 脚本时,如下所示:
[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
}
答案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