shell自动发布脚本
由于本地web服务器上部署了多个应用,如果单个应用发布的情况下,以前的shell脚本不能满足自动化要求:
[[ $1 == "api" ]]&&{
des1=/opt/vhosts/open
for m in 192.168.1.$3;do
echo "sending file $2 to remote host:192.168.1.$m:$des1"
scp -P 65422 src/$2 $m:/tmp
echo "stop tomcat service first ..."
#####步骤:1,kill java 进程,2,将原有war包进行更名,3,将传送过来的war包放入正确路径,4 将历史war包迁移备份至/opt/backup/,将 ROO
T 目录移动至/tmp/目录 5 删除缓存,6 启动服务
ssh -p 65422 -t $m<<UPDATE_API
[ -d /opt/backup ]||{
mkdir -p /opt/backup &&chown -R test:test /opt/backup/&&chmod 775 /opt/backup
}
pkill -9 java || echo 'kill process fail'
mv $des1/ROOT.war /opt/backup/ROOT.war.`date +%Y%m%d-%T`||echo mv1 fail
mv /tmp/$2 $des1/ROOT.war ||echo mv2 fail
mv /opt/vhosts/open/ROOT /tmp/bak.ROOT.`date +%Y%m%d-%T`
rm -rf /opt/apache-tomcat-6.0.35-open/work/Catalina ||echo clean cache fail
source /etc/profile
cd /opt/apache-tomcat-6.0.35-open/ && bin/startup.sh && echo $m update ok||echo `date` $m update fail
UPDATE_API
done
}
如上所示,采用了pkill -9 java 把所有的java进程杀死了。
进行脚本改造,增加判断:
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4245850&pid=24568042&page=1&extra=page%3D1#pid24568042发现判断出来的进程id并不是远端服务的进程,还没找到原因~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
也许可以采用#ps aux|grep test | grep -v grep|awk '{print $2}' | xargs kill -9 来直接替代
含泪废弃这种方式:
采用 ssh -t $user@ip "cmd.sh"
在本地服务器上创建脚本:
#!/bin/bash
#下载文件 本地保存路径
localdir=/home/test/deploy/src/
wget -c -N -T 10 ftp://$user:$password@deploy.test.tst/$2 -O $localdir$2||exit 1
#远端备份目录
backupdir=/opt/backup
#远端程序存放目录
package_tmp=/opt/package_tmp
#test
test=(238)
#m
m=192.168.16.
case $1 in
test)
for ip in ${test[*]}
do
#echo $ip
scp -P22 $localdir$2 $m$ip:$package_tmp
ssh -p22 -t $m$ip "/usr/local/src/cmd.sh"
done
;;
esac
应用服务器上脚本:
#!/bin/bash
backupdir=/opt/backup
testdes=/opt/vhosts/test
#默认程序包名
package=ROOT.war
#临时程序本地存储目录
package_tmp=/opt/package_tmp
#默认web程序包读取跟目录
webpackage_dir=/opt/vhosts
if [ ! -d $backupdir ]
then
mkdir -p $backupdir && chown -R winupon:winupon $backupdir && chmod 775 $backupdir
fi
if [ ! -d $package_tmp ]
then
mkdir -p $package_tmp && chown -R winupon:winupon $package_tmp && chmod 775 $package_tmp
fi
#获取package_tmp下最新的文件
warname=`ls -t $package_tmp | awk '/war/{if(NR==1){print $filename}}'`
echo "warname is:" $warname
if [ -z $warname ]; then
echo "Package does not exist,please check!!!"
exit 0
#else
# mv $warname
fi
#停服务
testtomcatdir_list=$(ps aux|grep test | grep -v grep | awk '{print $12}'| sed -e 's/.*=\(.*\)\/conf.*/\1/g')
echo "testtomcatdir_list---------------" $testtomcatdir_list
if [ -z ${#testtomcatdir_list} ]; then
echo "tomcat command is not running,please check!!!"
exit 0
fi
testid_list=`ps aux|grep test | grep -v grep|awk '{print $2}'`
for testpid in $testid_list;do
echo "tomcat command is running, pid:"${testpid}
kill -9 $testpid && echo kill1 OK || echo kill0 webtomcat fail
done
#备份
echo "begin to backup war---------------------"
mv $testdes/$package $backupdir/$package.`date +%Y%m%d-%T` && echo backup OK || echo mv1 fail
#放程序包到各个目录
testdesdir_list=`ls /opt/vhosts/ | grep test`
for testdesdir in $testdesdir_list; do
rm -rf $webpackage_dir/$testdesdir/* && echo rm1 OK || echo rm1 fail
cp $package_tmp/$warname $webpackage_dir/$testdesdir/$package && echo cp1 OK || echo cp1 fail
done
#清除缓存
echo "begin to update tomcat------------------"$testtomcatdir_list
for testtomcatdir in $testtomcatdir_list;do
rm -rf $testtomcatdir/work/Catalina && echo clean cache ok || echo clean cache fail
sleep 1
set -m;sh $testtomcatdir/bin/startup.sh && echo update ok || echo update fail =============》》为什么要加set -m ,如果不加,执行完这条命令,就退出了进程。结果服务也杀死了。
done
执行结果:
Address 192.168.16.238 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
docFactory_Web_exploded.war 100% 26MB 25.9MB/s 00:01
+ ssh -p22 -t 192.168.16.238 /usr/local/src/cmd.sh
Address 192.168.16.238 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
warname is: docFactory_Web_exploded.war
testtomcatdir_list--------------- /opt/apache-tomcat-6.0.35_test1 /opt/apache-tomcat-6.0.35_test
tomcat command is running, pid:28761 begin to kill
kill1 OK
tomcat command is running, pid:28791 begin to kill
kill1 OK
begin to backup war---------------------
backup OK
rm1 OK
cp1 OK
rm1 OK
cp1 OK
begin to update tomcat------------------/opt/apache-tomcat-6.0.35_test1 /opt/apache-tomcat-6.0.35_test
clean cache ok
Using CATALINA_BASE: /opt/apache-tomcat-6.0.35_test1
Using CATALINA_HOME: /opt/apache-tomcat-6.0.35_test1
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.35_test1/temp
Using JRE_HOME: /opt/jdk1.6.0_38/
Using CLASSPATH: /opt/apache-tomcat-6.0.35_test1/bin/bootstrap.jar
update ok
clean cache ok
Using CATALINA_BASE: /opt/apache-tomcat-6.0.35_test
Using CATALINA_HOME: /opt/apache-tomcat-6.0.35_test
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.35_test/temp
Using JRE_HOME: /opt/jdk1.6.0_38/
Using CLASSPATH: /opt/apache-tomcat-6.0.35_test/bin/bootstrap.jar
update ok
Connection to 192.168.16.238 closed.