我正在尝试编写一个简短的bash脚本来自动加载战争,以便保留我在tomcat服务器上的配置文件。但是,有时当我尝试在脚本中停止tomcat时,它会给我这个错误:
Using CATALINA_BASE: /usr/local/apache-tomcat-6.0.32~
Using CATALINA_HOME: /usr/local/apache-tomcat-6.0.32~
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-6.0.32~/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-6.0.32~/bin/bootstrap.jar
May 31, 2011 8:41:49 AM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at java.net.Socket.<init>(Socket.java:392)
at java.net.Socket.<init>(Socket.java:206)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:422)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:338)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:416)
我最初认为这是因为我在完全启动它之前试图关闭服务器,而且可能仍然如此。但是,我在脚本中添加了一个部分,用于在关闭服务器之前检查以确保服务器已启动。然后,因为我有时会得到错误,我增加了一些睡眠时间。如果我让它睡10秒钟,它似乎一直工作,但如果我让它只睡5,它就不那么一致。我认为这归结于我不熟悉tomcat的启动和关闭顺序,所以我不确定我的问题是否是我认为的问题,我不知道什么是一个好的,万无一失的解决方案。 因此,我的问题是:导致此错误的原因是什么,如果它是由服务器未完全启动引起的,我的代码的哪些更改可以防止错误?
非常感谢你的帮助! MirroredFate
我的剧本:
#!/bin/bash
# Script to redeploy war, needs path to the new war
if [ ! -f $1 ];
then
echo "File $1 not found! Get it right this time."
exit 0;
fi
#Check if tomcat is up, if it is, shut 'er down
RESULT=`netstat -na | grep 8080 | awk '{print $7}' | wc -l`
if [ "$RESULT" = 0 ]; then
echo "Tomcat already down"
elif [ "$RESULT" != 0 ]; then
echo "Shutting down tomcat"
./shutdown.sh;
fi
cp ../webapps/SQLLoader/conf/configurable.properties ..;
rm ../webapps/SQLLoader -R;
echo "SQLLoader dir removed";
rm ../webapps/SQLLoader.war;
echo "SQLLoader removed";
cp $1 ../webapps;
echo "$1 moved to tomcat";
./startup.sh;
wait
echo "Server started";
echo "Waiting for dir to be created..."
while [ ! -d "../webapps/SQLLoader/conf" ]; do
# Control will enter here if directory doesn't exist
sleep 1
done;
mv ../configurable.properties ../webapps/SQLLoader/conf/;
echo "Config file put back in place";
RESULT=`netstat -na | grep 8080 | awk '{print $7}' | wc -l`
while [ "$RESULT" = 0 ]; do
echo "Waiting for tomcat to start... JUST SO WE CAN KILL IT AGAIN HAHAHAHAHA!!"
sleep 0.5
RESULT=`netstat -na | grep 8080 | awk '{print $7}' | wc -l`
done;
echo "Sleep for safety"
sleep 5;
echo "Shutting down again"
./shutdown.sh
echo "Starting... again...... yeah..."
./startup.sh
echo "Status is: $?"
答案 0 :(得分:1)
因此,我发现我的具体问题的解决方案是简单地使用jar命令(duh)来解压战而不是重新启动服务器。这是我的新代码:
#!/bin/bash
# Script to redeploy war, needs path to the new war
if [ ! -f $1 ];
then
echo "File $1 not found! Get it right this time."
exit 0;
fi
#Check if tomcat is up, if it is, shut 'er down
RESULT=`netstat -na | grep $2 | awk '{print $7}' | wc -l`
if [ "$RESULT" = 0 ]; then
echo "Tomcat already down"
elif [ "$RESULT" != 0 ]; then
echo "Shutting down tomcat"
./shutdown.sh;
fi
cp ../webapps/SQLLoader/conf/configurable.properties ..;
rm ../webapps/SQLLoader -R;
echo "SQLLoader dir removed";
rm ../webapps/SQLLoader.war;
echo "SQLLoader removed";
cp $1 ../webapps;
echo "$1 moved to tomcat";
mkdir ../webapps/SQLLoader/
cd ../webapps/SQLLoader/
jar -xvf ../SQLLoader.war;
echo "Waiting for dir to be created..."
while [ ! -d "conf" ]; do
# Control will enter here if directory doesn't exist
sleep 1
done;
echo "Directory created, moving properties file";
cd ../../bin;
mv ../configurable.properties ../webapps/SQLLoader/conf/;
echo "Starting up"
./startup.sh
echo "Status is: $?"
它没有回答如何最终阻止错误的问题,但它确实解决了我的微观世界中的问题。如果有人想出为什么在之前解释的情况下发生错误的原因,请发布,因为我相信人们仍然有兴趣知道答案。