一键重启Linux系统下的java web项目和java应用程序的shell脚本

鹰之翔 2015-02-10

这几天做项目迁移,需要将之前在Win下面的java web项目和java应用程序迁移到Linux系统下面去,迁移的原因就不多说了,无非是linux系统安全,稳定,也不为怪,毕竟是做舆情的,信息也挺重要的哈!

在开始前想给大家推荐个好玩的桌游,名字叫做UNO,额?不知道啥意思,找度娘去吧,我每天打开百度的次数不下玉五百次的,作为程序员经常和大家玩这个的,每次都要笑出病了,嘻嘻!

正题:

在下面的脚本中写了两个java web项目,其实是两个tomcat啦,第三个是java应用,对于web项目在使用时只要修改前面的三个变量就行了:TOMCAT_PATH,TOMCAT_BIN,TOMCAT_PORT,对应着tomcat的所在路径,所在的bin目录,还有就是项目的端口号。

对于java项目只需修改:APP_PATH,APP_HOME,APP_MAINCLASS就行,一看就明白,不多说。直接

上shell代码(ps:代码很详细的喽):

#!/bin/sh 
#a key to restart all tomcats

#set static variable start
	#all static variable start
	TOMCAT_USER=root
	#all static variable end

	#1.ahsx start
	TOMCAT_PATH_QGBBS=/data/iCrawlerS_qgBBS1
	TOMCAT_BIN_QGBBS=/data/iCrawlerS_qgBBS1/bin 
	TOMCAT_PORT_QGBBS=2616
	#1.ahsx end
	
	#2.gd start
	TOMCAT_PATH_QGBBS2=/data/iCrawlerS_qgBBS2
	TOMCAT_BIN_QGBBS2=/data/iCrawlerS_qgBBS2/bin 
	TOMCAT_PORT_QGBBS2=3128
	#2.gd end
	
	
	#添加Java 应用程序 START
	#all static variable start
	JAVA_HOME="/opt/jdk1.6.0_26"
	RUNNING_USER=root
	JAVA_OPTS="-ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"
	#all static variable endjps
	
	
	#4.imonitor程序 START
	APP_PATH_MONITOR=/data/Monitorprogram
	APP_HOME_MONITOR=/data/Monitorprogram/WebRoot/WEB-INF
	APP_MAINCLASS_MONITOR=com.judge.Filetime
	CLASSPATH_MONITOR=$APP_HOME_MONITOR/classes
	for i in "$APP_HOME_MONITOR"/lib/*.jar;do
		CLASSPATH_MONITOR="$CLASSPATH_MONITOR":"$i"
	done
	psid_monitor=0
	#4.imonitor程序 END
	
	
	
	#添加Java 应用程序 END
	
#set static variable end

#1.ahsx start function start
start_qgbbs(){
	stop_qgbbs 
	su - $TOMCAT_USER -c $TOMCAT_BIN_QGBBS/startup.sh 
	sleep 3 
	if netstat -an | grep $TOMCAT_PORT_QGBBS >/dev/null 
	then 
	echo "Tomcat is running" 
	return 0 
	else 
	return 1 
	fi 
}

stop_qgbbs(){
	if netstat -an | grep $TOMCAT_PORT_QGBBS | grep LISTEN >/dev/null 
	then 
	# try to shutdown the server first 
	echo "Now Shutdown the server..." 
	su - $TOMCAT_USER -c  $TOMCAT_BIN_QGBBS/shutdown.sh 
	else 
	echo "Tomcat is not running" 
	return 0 
	fi 

	if netstat -an | grep $TOMCAT_PORT_QGBBS | grep LISTEN 
	then 
	sleep 10 
	echo "Now kill it" 	
	ps -ef | grep java | grep $TOMCAT_PATH_QGBBS | awk '{print "kill -9 ", $2;}' | sh 
	fi 

	if netstat -an | grep $TOMCAT_PORT_QGBBS | grep LISTEN >/dev/null 
	then 
	return 1 # still alive 
	else 
	echo "Tomcat is stopped" 
	return 0 
	fi 
}
#1.qqbbs start function end

#2.qqbbs2 start function start
start_qgbbs2(){
	stop_qgbbs2 
	su - $TOMCAT_USER -c $TOMCAT_BIN_QGBBS2/startup.sh 
	sleep 3 
	if netstat -an | grep $TOMCAT_PORT_QGBBS2 >/dev/null 
	then 
	echo "Tomcat is running" 
	return 0 
	else 
	return 1 
	fi 
}

stop_qgbbs2(){
	if netstat -an | grep $TOMCAT_PORT_QGBBS2 | grep LISTEN >/dev/null 
	then 
	# try to shutdown the server first 
	echo "Now Shutdown the server..." 
	su - $TOMCAT_USER -c  $TOMCAT_BIN_QGBBS2/shutdown.sh 
	else 
	echo "Tomcat is not running" 
	return 0 
	fi 

	if netstat -an | grep $TOMCAT_PORT_QGBBS2 | grep LISTEN 
	then 
	sleep 10 
	echo "Now kill it" 	
	ps -ef | grep java | grep $TOMCAT_PATH_QGBBS2 | awk '{print "kill -9 ", $2;}' | sh 
	fi 

	if netstat -an | grep $TOMCAT_PORT_QGBBS2 | grep LISTEN >/dev/null 
	then 
	return 1 # still alive 
	else 
	echo "Tomcat is stopped" 
	return 0 
	fi 
}
#2.qqbbs2 start function end

#4.monitor start function start
checkpid_monitor(){
	javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAINCLASS_MONITOR`
	
	if [ -n "$javaps" ]; then
	    psid_monitor=`echo $javaps | awk '{print $1}'`
	else 
	    psid_monitor=0
	fi
	echo "$psid_monitor"


}

start_monitor(){
	stop_monitor
	checkpid_monitor
	if [ $psid_monitor -ne 0 ];then
		echo "warn: $APP_MAINCLASS_MONITOR already started! (pid=$psid_monitor)"
	else
	     	echo -n "Starting $APP_MAINCLASS_MONITOR"
		
		nohup java -cp "$APP_HOME_MONITOR/lib/*:$APP_HOME_MONITOR/classes" $APP_MAINCLASS_MONITOR $APP_PATH_MONITOR > $APP_HOME_MONITOR/log.out &
 	
	checkpid_monitor
		if [ $psid_monitor -ne 0 ];then
			echo "(pid=$psid_monitor) [OK]" 
		else 
			echo "[Failed]"
		fi
	fi

}
stop_monitor(){
	checkpid_monitor
	if [ $psid_monitor -ne 0 ]; then
		echo -n "Stopping $APP_MAINCLASS ...(pid=$psid_monitor)"
		kill -9 $psid_monitor
		if [ $? -eq 0 ];then
			echo "OK"
		else 
			echo "[Failed]"
		fi
	
		checkpid_monitor
		if [ $psid_monitor -ne 0 ];then
			stop
		fi
	else
		echo "===================================="
		echo "warn: $APP_MAINCLASS_MONITOR is not running " 
		echo "===================================="
	fi
	
}
#4.monitor start function end

#the place of program start
start_qgbbs
start_qgbbs2

start_monitor
#the place of program end

相关推荐