gpt4 book ai didi

linux - 使用 bash shell 脚本启动和停止多个 tomcat

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:29 26 4
gpt4 key购买 nike

我写了这个 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

最佳答案

通常你可以通过引用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

这是一个例子:

https://gist.github.com/miglen/5590986

关于linux - 使用 bash shell 脚本启动和停止多个 tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28852455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com