我写了这个sh脚本来停止并在ubuntu终端上用这个命令启动多个tomcat
sh apacheStopStartScript.sh start.
通过启动此脚本,将在那里创建我的apacheStopStartScript.sh脚本的日志文件,但我希望日志将在相应的tomcat的bin / logs文件夹中创建。
任何人解决此问题?
#!/bin/bash
# script Name: apacheStopStartScript.sh
# Script is run by command: sh apacheStopStartScript.sh start
### END INIT INFO
TOMCATNAME=""
TOMCATCOMMANPATH="/root/siteforge/"
TOMCATFIRST="apache-two-siteforge"
TOMCATSECOND="apache-three-siteforge"
TOMCATTHIRD="apache-four-siteforge"
TOMCATFOURTH="apache-five-siteforge"
TOMCATFIFTH="apache-six-siteforge"
NOW=""
LOG=""
check=""
count=0
# This Function Is Used To Find Running Tomcat Process Id
tomcat_pid() {
echo `ps -efa | grep $TOMCATNAME | grep tomcat | grep java | egrep -v grep | awk '{print $2}'`
}
# This Function Is Used To Run/Start TOMCAT
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "TOMCAT : $TOMCATNAME is already running with (processId: $pid)"
else
echo `nohup $TOMCATCOMMANPATH$TOMCATNAME/bin/catalina.sh run>>$LOG &`
# echo `nohup catalina.sh run>>logs/today.log &`
echo "\n"
check=$(cat $LOG | grep "Server startup in" | wc |tr -s ' ' | cut -d ' ' -f 4)
while [ $check -eq $count ]
do
echo "Starting TOMCAT : $TOMCATNAME "
check_count=$(cat $LOG | grep "Server startup in" | wc |tr -s ' ' | cut -d ' ' -f 4)
check=`expr $check + $check_count `
sleep 25
if [ $check -gt $count ]
then
break
fi
done
cat $LOG
echo "\n -------------------------TOMCAT :$TOMCATNAME STARTED SUCCESSFULLY----------------------------"
echo "\n------------------------------------------------------------------------------------------------"
echo `ps -efa | grep tomcat`
fi
return 0
}
# This Function Is Used To Stop TOMCAT
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Killing TOMCAT : $TOMCATNAME processes with (processId: $pid)"
kill -9 $(tomcat_pid)
echo "\n -------------------------STOPPED TOMCAT :$TOMCATNAME ---------------------------------------"
else
echo "TOMCAT : $TOMCATNAME is not running"
fi
return 0
}
# This Function Is Used To terminate/kill running Tomcat by process Id
terminate() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Killing processes with (processId: $pid)"
kill -9 $(tomcat_pid)
else
echo "No Such Process Is Running"
fi
return 0
}
# These are calling function name i.e start, stop, restart,kill,status
case $1 in
start)
TOMCATNAME=$TOMCATFIRST
NOW="$(date +'%Y-%m-%d-%T')"
LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
stop
sleep 10
start
read -p "Are you sure you want to start $TOMCATSECOND? <Y/N> " prompt1
case $prompt1 in
[Yy]*)
TOMCATNAME=$TOMCATSECOND
NOW="$(date +'%Y-%m-%d-%T')"
LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
stop
sleep 10
start
;;
[Nn]*)
echo "Successfully Exit"
exit;;
esac
TOMCATNAME=$TOMCATTHIRD
NOW="$(date +'%Y-%m-%d-%T')"
LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
stop
sleep 10
start
TOMCATNAME=$TOMCATFOURTH
NOW="$(date +'%Y-%m-%d-%T')"
LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
stop
sleep 10
start
TOMCATNAME=$TOMCATFIFTH
NOW="$(date +'%Y-%m-%d-%T')"
LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
stop
sleep 10
start
;;
stop)
stop
;;
restart)
stop
start
;;
kill)
terminate
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "TOMCAT : $TOMCATNAME is running with processId: $pid"
else
echo "TOMCAT : $TOMCATNAME is not running"
fi
;;
esac
exit 0
答案 0 :(得分:1)
通常,您可以通过引用catalina.sh为您停止/启动您的tomcat来让您的生活更轻松:
所以在你的开始过程中:
su - $TOMCAT_USER -c "$TOMCAT_PATH/catalina.sh start"
在catalina.sh中你会看到:
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi
这是一个例子: