Vagho Vagho - 4 months ago 18
Linux Question

installing tomcat and java on centos using bash

i am having a problem starting the tomcat service properly after installing it. i am using centos 7 bash 4.

if the tomcat user and the tomcat service name are the same my script works fine, and gets the service pid at the end, but if they are different then something goes wrong and the service does not work properly and it does not get service pid.

here are the scripts that i am using.

can anyone tell me what could be the reason for this conflict?

thanks .

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 ""





faulty tomcat ps aux result



root 50855 48981 0 07:00 pts/0 00:00:00 grep --color=auto tomcatSvc





working tomcat ps aux result



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/tomcat802/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/tomcat802/endorsed -classpath /opt/pilot/tomcat802/bin/bootstrap.jar:/opt/pilot/tomcat802/bin/tomcat-juli.jar -Dcatalina.base=/opt/pilot/tomcat802 -Dcatalina.home=/opt/pilot/tomcat802 -Djava.io.tmpdir=/opt/pilot/tomcat802/temp org.apache.catalina.startup.Bootstrap start
app 23441 23408 0 09:16 pts/0 00:00:00 grep tomcat802




Answer

The key to understand your code is this command:

tomcat_pid() {
    echo `ps aux | ps -ef | grep $TOMCAT_SVC | grep java | awk ' { print $2 } '`
}

It should/could be:

tomcat_pid() {
    echo `ps aux | ps -ef | grep $tomcatDirName | grep java | awk ' { print $2 } '`
}

Explanation:

The ps command lists all running processes (including their paths), while grep filter that list based on your "keyword" (in your current code, it's $TOMCAT_SVC/tomcatSvc).

Looking at your "working tomcat ps aux result", I can see that the application is started in folder tomcat802. Furthermore, there is nothing called "tomcatSvc" inside the path:

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/tomcat802/conf/logging.properties -Djava.util.logging.manager=org.apache.juli...

Therefore, if you keep the original command, the system will NOT find the correct PID (simply because there's no literal "tomcatsvc" in your executable path) If you change to my recommended command, the system will find it.

Warning: this way of finding PID is kind of dangerous, since if you have another running program with path containing "tomcat802", that program could be selected instead. You want to put the absolute path instead of tomcat802; and make sure no one move that folder, or else the code might break.

Comments