I am attempting to write a short bash script to auto-load a war in such a way as to preserve the config file I have on the tomcat server. However, sometimes when I attempt to stop tomcat within the script, it gives me this error:
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)
I initially thought this was because I was attempting to shut down the server before I had fully started it up, and that may still be the case. However, I I added a section to the script that checks to make sure the server is up before shutting it down. Then, because I would still get the error sometimes, I added some sleep time. If I make it sleep for 10 seconds, it seems to work consistently, but if I make it only sleep for 5, it is not as consistent. I think this comes down to me just not being very familiar with the startup and shutdown sequencing of tomcat, so I am unsure if my problem is even what I think it is, and I have no idea what a good, foolproof solution is. Thus, my question is: What causes this error, and if it is caused by the server not being fully up, what changes to my code could prevent the error?
Thanks so much for the help! MirroredFate
My script:
#!/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`
开发者_如何学Cwhile [ "$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: $?"
So, the solution I found to my specific problem is to simply use the jar command (duh) to unpack the war rather than restarting the server. Here is my new code:
#!/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: $?"
It doesn't answer the question of how to definitively prevent one from getting that error, but it does solve the problem within my microcosm. If someone comes up with a reason as to why that error occurred in the situation previously explained, please post it as I am sure that people will still be interested in knowing the answer.
精彩评论