我在安装后正确启动tomcat服务时遇到问题。我正在使用centos 7 bash 4。
如果 tomcat用户和 tomcat服务名称相同,我的脚本工作正常,并在最后得到服务pid,但如果它们不同那么出错了,服务无法正常运行,也没有服务pid。
这是我正在使用的脚本。
谁能告诉我这场冲突可能是什么原因?谢谢。
deployScript.sh
#!/bin/bash
tomcatDirName="tomcat802"
tomcatSvcName="tomcatSvc"
tomcatSvcUsr="tomcatUsr"
tomcatSvcGrp="tomcatGrp"
installationPath="/opt/app"
javaDirName="java"
javaDirPath="$installationPath/$javaDirName"
jdkDirPath="$javaDirPath/jdk1.8.0_45"
userHomePath="$installationPath/$tomcatSvcUsr"
tomcatDirPath="$installationPath/$tomcatDirName"
tomcatConfPath="$tomcatDirPath/conf"
tomcatLogsPath="$tomcatDirPath/logs"
tomcatBinPath="$tomcatDirPath/bin"
tomcatLogsTomcat="$tomcatLogsPath/tomcat"
tomcatLogsAccess="$tomcatLogsPath/access"
setEnvShPath="$tomcatBinPath/setenv.sh"
catalinaShSearch='CATALINA_OUT="$CATALINA_BASE.*'
catalinaShReplace='CATALINA_OUT="$CATALINA_BASE"/logs/tomcat/catalina.out'
catalinaShPath="$tomcatBinPath/catalina.sh"
initDTomcatFilePath="/etc/init.d/$tomcatSvcName"
catalinaLogsSearch='${catalina.base}/logs'
catalinaLogsReplace='${catalina.base}/logs/tomcat'
loggingPropertiesPath="$tomcatConfPath/logging.properties"
serverXMLPath="$tomcatConfPath/server.xml"
maxPostSize="15728640"
##Download Tomcat
wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.21/bin/apache-tomcat-8.0.21.tar.gz
# Download JDK
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz"
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
rpm -ihv rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
yum -y install epel-release
yum -y install xmlstarlet
yum -y install htop
mkdir -p $tomcatDirPath
mkdir -p $javaDirPath
mkdir -p $userHomePath
tar -xzf jdk-8u45-linux-x64.tar.gz -C $javaDirPath
rm -f $jdkDirPath/javafx-src.zip
rm -f $jdkDirPath/src.zip
tar -xzf apache-tomcat-8.0.21.tar.gz -C $tomcatDirPath --strip-components=1
rm -rf $tomcatDirPath/webapps/docs
rm -rf $tomcatDirPath/webapps/examples
rm -rf $tomcatDirPath/webapps/host-manager
rm -rf $tomcatDirPath/webapps/ROOT/*
# add user and permissions
groupadd $tomcatSvcGrp
useradd -M -s /sbin/nologin -g $tomcatSvcGrp -d $userHomePath $tomcatSvcUsr
findAndReplace() {
declare -A TomcatInitD=(
["^CATALINA_HOME.*"]="CATALINA_HOME=$tomcatDirPath"
#["^TOMCAT_USER.*"]="TOMCAT_USER=$tomcatSvcUsr"
#["^TOMCAT_SVC.*"]="TOMCAT_SVC=$tomcatSvcName"
["^export JAVA_HOME=.*"]="export JAVA_HOME=$jdkDirPath"
)
for i in "${!TomcatInitD[@]}"
do
value="${TomcatInitD[$i]}"
key="$i"
sed -i -e "s~$key~$value~" $initDTomcatFilePath
done
}
cp tomcat801_init.d.txt $initDTomcatFilePath
cp setenv.sh $setEnvShPath
if [ -f $initDTomcatFilePath ]
then
findAndReplace
else
cp tomcat801_init.d.txt $initDTomcatFilePath
findAndReplace
fi
chown $tomcatSvcUsr $initDTomcatFilePath
chgrp $tomcatSvcGrp $initDTomcatFilePath
chmod g+rwx $initDTomcatFilePath
chown $tomcatSvcUsr $tomcatDirPath
chgrp -R $tomcatSvcGrp $tomcatDirPath
chmod g+rwx $tomcatDirPath
# i have no idea why is it for and why it is not working
#chkconfig --add $tomcatSvcName
#chkconfig --level 234 $tomcatSvcName on
cd $installationPath
chown -R $tomcatSvcUsr *
chgrp -R $tomcatSvcGrp *
chmod g+rwx $tomcatConfPath
cd $tomcatConfPath
chmod g+r *
cd $installationPath
# modify tomcat logging path in conf\logging.properties by adding tomcat folder ${catalina.base}/logs/tomcat
sed -i -e "s~$catalinaLogsSearch~$catalinaLogsReplace~" $loggingPropertiesPath
mkdir $tomcatLogsTomcat
chown -R $tomcatSvcUsr $tomcatLogsTomcat
chgrp -R $tomcatSvcGrp $tomcatLogsTomcat
# modify server.xml set acces log path to logs/access
xmlstarlet ed -L -u /Server/Service/Engine/Host/Valve[@directory]/@directory -v "logs/access" $serverXMLPath
mkdir $tomcatLogsAccess
chown -R $tomcatSvcUsr $tomcatLogsAccess
chgrp -R $tomcatSvcGrp $tomcatLogsAccess
# modify server.xml add maxPostSize tags to http and ajp connectors
xmlstarlet ed -L -a '/Server/Service/Connector[@name="b"]' -t 'elem' -n 'maxPostSize' -v 0 -i '/Server/Service/Connector[not(@name)]' -t 'attr' -n 'maxPostSize' -v "$maxPostSize" $serverXMLPath
#edit tomcat801/bin/catalina.sh
#line 199 change catalina.out file location to
#CATALINA_OUT="$CATALINA_BASE"/logs/tomcat/catalina.out
sed -i -e "s~$catalinaShSearch~$catalinaShReplace~" $catalinaShPath
JAVA_HOME="$jdkDirPath"
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
#Add native library for PROD env. to speed up tomcat startup
yum -y install apr-devel openssl-devel
cd $tomcatBinPath
tar -xvzf tomcat-native.tar.gz
cd tomcat-native-1.1.33-src/jni/native
yum -y install gcc
./configure --with-apr=/usr && make && sudo make install
cd /usr/lib
rm -f libtcnative-1.so
ln -s /usr/local/apr/lib/libtcnative-1.so libtcnative-1.so
chown -h $tomcatSvcUsr libtcnative-1.so
chgrp -h $tomcatSvcGrp libtcnative-1.so
cd $tomcatBinPath
rm -rf tomcat-native-1.1.33-src/
#yum -y remove gcc
#yum -y remove apr-devel
#yum -y remove openssl-devel
#yum -y remove epel-release
#yum -y remove xmlstarlet
service $tomcatSvcName start
service $tomcatSvcName status

tomcat801_init.d.txt
#!/bin/bash
#
# tomcat801
#
# chkconfig: - 234 80 20
#
### BEGIN INIT INFO
# Provides: tomcat801
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Tomcat 801
# Short-Description: start and stop tomcat
### END INIT INFO
## Source function library.
#. /etc/rc.d/init.d/functions
export JAVA_HOME=/opt/app/java/jdk1.8.0_45
export JAVA_OPTS="-Dfile.encoding=UTF-8"
export PATH=$JAVA_HOME/bin:$PATH
CATALINA_HOME=/opt/app/tomcat801
TOMCAT_USER=tomcatUsr
TOMCAT_SVC=tomcatSvc
SHUTDOWN_WAIT=20
tomcat_pid() {
echo `ps aux | ps -ef | grep $TOMCAT_SVC | grep java | awk ' { print $2 } '`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat"
ulimit -n 100000
umask 007
/bin/su -p -s /bin/sh $TOMCAT_USER $CATALINA_HOME/bin/startup.sh
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stoping Tomcat"
/bin/su -p -s /bin/sh $TOMCAT_USER $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 "\nwaiting for processes to exit";
sleep 1
let count=$count+1;
echo ""
done
if [ $count -gt $kwait ]; then
echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0

setenv.sh
#! /bin/bash
export CATALINA_OPTS="$CATALINA_OPTS -Xms1024m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:NewSize=512m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxNewSize=512m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseParallelGC"
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxGCPauseMillis=1500"
export CATALINA_OPTS="$CATALINA_OPTS -XX:GCTimeRatio=9"
export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
export CATALINA_OPTS="$CATALINA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
export CATALINA_OPTS="$CATALINA_OPTS -server"
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"
if [ -r "$CATALINA_BASE/bin/appenv.sh" ]; then
. "$CATALINA_BASE/bin/appenv.sh"
fi
echo "Using CATALINA_OPTS:"
for arg in $CATALINA_OPTS
do
echo ">> " $arg
done
echo ""
echo "Using JAVA_OPTS:"
for arg in $JAVA_OPTS
do
echo ">> " $arg
done
echo "_______________________________________________"
echo ""

错误的tomcat ps aux结果
root 50855 48981 0 07:00 pts/0 00:00:00 grep --color=auto tomcatSvc
工作tomcat ps aux结果
502 687 1 3 May31 ? 1-17:06:35 /opt/pilot/java/jdk1.8.0_45/bin/java -Djava.util.logging.config.file=/opt/pilot/tomcatSvc/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dfile.encoding=UTF-8 -Xms6000m -Xmx6000m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:+UseParallelGC -XX:MaxGCPauseMillis=1500 -XX:GCTimeRatio=9 -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -server -XX:+DisableExplicitGC -Djava.endorsed.dirs=/opt/pilot/tomcatSvc/endorsed -classpath /opt/pilot/tomcatSvc/bin/bootstrap.jar:/opt/pilot/tomcatSvc/bin/tomcat-juli.jar -Dcatalina.base=/opt/pilot/tomcatSvc-Dcatalina.home=/opt/pilot/tomcatSvc-Djava.io.tmpdir=/opt/pilot/tomcatSvc/temp org.apache.catalina.startup.Bootstrap start
app 23441 23408 0 09:16 pts/0 00:00:00 grep tomcatSvc
答案 0 :(得分:1)
理解你的代码的关键是这个命令:
tomcat_pid() {
echo `ps aux | ps -ef | grep $TOMCAT_SVC | grep java | awk ' { print $2 } '`
}
应该/可能是:
tomcat_pid() {
echo `ps aux | ps -ef | grep $tomcatDirName | grep java | awk ' { print $2 } '`
}
<强>解释强>
ps
命令列出所有正在运行的进程(包括其路径),而grep
根据您的“关键字”过滤该列表(在您当前的代码中,它是$ TOMCAT_SVC / tomcatSvc)。
看看你的“工作tomcat ps aux结果”,我可以看到应用程序是在tomcat802文件夹中启动的。此外,路径中没有任何名为“tomcatSvc”的内容:
502 687 1 3月31日? 1-17:06:35 /opt/pilot/java/jdk1.8.0_45/bin/java -Djava.util.logging.config.file = /选择/导频/ tomcat802 / CONF / logging.properties -Djava.util.logging.manager = org.apache.juli ...
因此,如果你保留原始命令,系统将找不到正确的PID(因为你的可执行路径中没有文字“tomcatsvc”) 如果您更改为我推荐的命令,系统将找到它。
警告:这种查找PID的方式很危险,因为如果你有另一个运行程序,其路径包含“tomcat802”,则可以选择该程序。你想把绝对路径而不是tomcat802;并确保没有人移动该文件夹,否则代码可能会中断。