每次在JBOSS发布ear包的时候,都需要敲一堆命令来完成。通过shell脚步,把所需的命令整合在一起,减少了工作量,同时也减少步骤出错(如:忘了把老的ear包备份)。
下面的Shell脚本完成了:杀进程、备份、发布和启动。如:在linux服务器下,/usr1/demo.ear是新的ear包,/usr1/jbosshome 是jboss的目录,/usr1/earbak是ear包备份的路径。启动时,输入ingling
#!/bin/bash#create by thd,use for auto publish jboss application#sourceEarPath: the new ear package path#jbosshome: the jboss root path#backupDir: use to backup old ear package #earPackName: the ear package name in jboss_home/server/default/deploysrc=""dest=""backup=""filename=""datestr=`date +"%Y%m%d"`if [ $# -ne 4 ]; then echo "Parameters: sourceEarPath jbosshome backupDir earPackName" exit 1else src="$1" dest="$2" backup="$3" filename="$4" ear=`echo "$filename" | cut -d "." -f2` if [ "$ear" != "ear" ]; then echo "earPackName suffix is $ear, must be 'ear' " exit 1 fifiecho "sourceEarPath=$src"echo "JBOSS_HOME=$dest"echo "backupDIR=$backup"echo "filename=$filename"if [ -f "$src/$filename" ]; then echo ""else echo "File not exits:$src/$filename" exit 1fi#EAR包是否在JBOSS的deploy目录if [ -f "$dest/server/default/deploy/$filename" ]; then echo ""else choice="n" echo "File not exists:$dest/server/default/deploy/$filename,continue?(y|n):" read choice case $choice in 'n'|'N') exit 1;; esac fi#确定备份的ear包名称tmpfilename="${filename%%.ear*}"if [ -f "$backup/$tmpfilename$datestr.ear" ]; then k=0 while [ -f "$backup/$tmpfilename$datestr-$k.ear" ] do let k=$k+1 done datestr="$datestr-$k"fiecho "Backup to : $backup/$tmpfilename$datestr.ear"#找出需要kill的JBOSS进程PID,通过awk查找PPID不为0的PID,同时需要过滤grep的进程#find the pid of jboss which is going to be killedprocess=`ps -ef | grep jboss |grep -v $0 | grep -v grep| awk 'BEGIN{a=0;}{if($3!=1) {a=$2;}else{a=0;} }END{printf("%d",a);}' `#循环等待killif [ "$process" != "0" ]; then echo "killing $process" kill $process t=0 i=0 while [ $process -gt 0 ] do #kill the process and wait for a moment sleep 1 #if the process of jboss is not be killed process=`ps -ef | grep jboss |grep -v $0 | grep -v grep| wc -l ` let i=$i+1 echo "$i s" #如果40秒还kill不了,就加上-9了 if [ $i -gt 40 ]; then if [ $t -eq 0 ]; then echo "Kill $process was failed(lasted 40 seconds),try kill forced." kill -9 $process i=20 t=1 else echo "kill was failed,exit" exit 1 fi fi done #backup the EAR package in jboss echo "cp $dest/server/default/deploy/$filename to $backup/$tmpfilename$datestr.ear" cp $dest/server/default/deploy/$filename $backup/$tmpfilename$datestr.ear #copy new EAR package to jboss echo "copy $src/$filename to $dest/server/default/deploy/$filename" cp -f $src/$filename $dest/server/default/deploy/$filename #clear the tmp folder echo "cleaning $dest/server/default/tmp/*" rm -rf $dest/server/default/tmp/* #clear the work folder echo "cleaning $dest/server/default/work/*" rm -rf $dest/server/default/work/* #start jboss echo "starting server" cd $dest/bin nohup sh $dest/bin/run.sh -c default 1>>$dest/bin/logs/start.log 2>&1 & sleep 10 tail -f $dest/bin/logs/start.logelse echo "Can't find the jboss PID" exit 1fi exit 0