这个脚本主要用于将java项目自动部署在linux平台的基础呀,这是第二版本。
已经做了相应修改,通过前面的变量,可以定义跨平台实施;而且在项目配置是源引其它文件的,可以更加灵活。
- #!/bin/bash
- #make by mojigan
- #time 2012-1019
- #version 1.4
- #upgrade the project
- #define some value.
- tomcat_program="/opt/tomcat/apache-tomcat-6.0.33_test"
- owner="testtomcat" #定义执行升级的用户
- location='/tmp/something/'
- function running(){
- b=''
- for ((i=0;i<=100;i+=2))
- do
- printf "progress:[%-50s]%d%%\r" $b $i
- sleep 0.2
- b=#$b
- done
- echo
- }
- function upgrade() {
- for pro1 in $*
- do
- #echo $pro1
- rundir="/data/webapps/${pro1}_apps/current"
- backdir="/data/webapps/${pro1}_apps/backup/$pro1"
- echo $rundir
- echo $backdir
- cp -R ${rundir} ${backdir}_$(date "+%Y%m%d_%H%M")
- cd ${rundir}
- pwd
- ls -l ${rundir} | awk '$0~/^-/ || $0~/^d/{print "rm -rf",$NF|"bash"}'
- mv ${location}${pro1}.war ${rundir}
- jar -xf ${rundir}/${pro1}.war
- echo "##########"
- echo "${pro1}"
- echo "##########"
- done
- }
- echo "+--------------------------------------------------------------+"
- echo "| === 确定特定用户执行,否则退出 === |"
- echo "+--------------------------------------------------------------+"
- user=$(id -un)
- if [ ${ user} == ${owner} ];then
- echo "Yes,we are the user of $owner "
- else
- echo "==>the program must run by $owner <=="
- exit 1
- fi
- echo ""
- echo ""
- echo "+--------------------------------------------------------------+"
- echo "| === 确认是否需要升级,升级项目数目,否则退出 === |"
- echo "+--------------------------------------------------------------+"
- result=$(find $location -type f -mtime -1 -user ${owner} -name "*.war")
- result1=$(echo $result | sed "s#$location##g;s#.war##g")
- if [ -z "$result1" ];then
- echo "We don't upgrade anything."
- exit 1
- else
- echo "When you run this script,it will upgrade the project $result1 "
- read -p "Would you like do that?(yes/no)" yesno
- if [ ${yesno} == "yes" ];then
- echo " Let's go,update the project. "
- else
- echo "You stop the bash script,we don't upgrade the project."
- exit 1
- fi
- fi
- echo ""
- echo ""
- echo "+--------------------------------------------------------------+"
- echo '| === 由于升级需要,准备杀掉tomcat进程,请慎重!!!!! === |'
- echo "+--------------------------------------------------------------+"
- echo "######################################"
- echo "##WARNING.I WANT TO KILL THE TOMCAT.##"
- echo "######################################"
- read -p "Would you like do that?(yes/no)" yesno
- if [ ${yesno} == "yes" ];then
- echo "Let's go,update the project. "
- echo "kill the tomcat service instance,please hold on 5 seconds........."
- kill $(pgrep -u testtomcat java)
- running
- running
- else
- echo "You stop the bash script,we don't kill the tomcat and update project ."
- exit 1
- fi
- echo ""
- echo ""
- echo "+--------------------------------------------------------------+"
- echo '| === 项目升级进行中,请稍候. we are running now. === |'
- echo "+--------------------------------------------------------------+"
- echo "通过调用函数,把相应的参数导入即可"
- echo "进行备份,删除,转移,解压等操作."
- upgrade $result1
- echo ""
- echo ""
- echo "+--------------------------------------------------------------+"
- echo '| === 复制替换修改配置文件 === |'
- echo "+--------------------------------------------------------------+"
- #cpp something.
- source /home/testtomcat/bin/configure.file
- echo ""
- echo ""
- echo "+--------------------------------------------------------------+"
- echo '| === 启动tomcat进程实例,大功告成,哈哈哈哈哈哈. === |'
- echo "+--------------------------------------------------------------+"
- #startup tomcat server
- ${tomcat_program}/bin/startup.sh
- echo ""
- echo ""
- echo "+--------------------------------------------------------------+"
- echo '| === 查看后台日志,了解事实的真相. === |'
- echo "+--------------------------------------------------------------+"
- echo "#view logs"
- running
- running
- tail -f ${tomcat_program}/logs/catalina.out