2011-05-31 34 views
1

我正在嘗試編寫一個簡短的bash腳本來自動加載一個戰爭,以保留我在tomcat服務器上的配置文件。但是,有時,當我試圖在腳本中停止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

我觀察過這種事情,但從來沒有找到原因。如果我趕時間,我只需要在上面放一個'kill -9'。 (我寫我的web應用程序以抵抗這種濫用......) – 2011-05-31 15:06:08

回答

1

因此,解決方案,我發現我的具體的問題是簡單地使用jar命令(杜)解壓戰爭,而不是重新啓動服務器。這是我的新代碼:

#!/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: $?" 

它並沒有回答如何防止明確從一個得到該錯誤的問題,但它確實解決了我的縮影中的問題。如果有人想出一個理由說明爲什麼在前面解釋的情況下出現了這種錯誤,請張貼它,因爲我確信人們仍然有興趣知道答案。