java项目启动脚本
我们公司由于是基于spring cloud开发的java项目,所以所有项目启动都是基于jar包形式的微服务启动的,下面的server_pwd.txt是配置的java启动参数
#!/bin/bash ## java env PATH_WEBAPPS=/opt/platform/ export JAVA_HOME=/opt/platform/tools/jdk1.8.0_111 export JRE_HOME=$JAVA_HOME/jre export JAR_NAME=`grep "$1" ${PATH_WEBAPPS}/server_pwd.txt|awk '{print $NF}'` LOG_NAME=`grep "$1" ${PATH_WEBAPPS}/server_pwd.txt|awk '{print $NF}' |grep -o "[^/]*$" |awk -F "-" '{print $3}' |cut -d"." -f1` DIRECTORY=`grep "$1" ${PATH_WEBAPPS}/server_pwd.txt|awk '{print $NF}' |cut -d"/" -f4` PATH_PWD=$(cd `dirname $0`;echo `pwd`) PATH_TO_JAR=${PATH_WEBAPPS}/$DIRECTORY/${JAR_NAME} PATH_LOG=/data/pdms_data/$LOG_NAME COMMAND_SS=`find /usr -type f -name "ss" | sed -n "1p" | tr -d " "` #PID_PATH_NAME=/var/run/${JAR_NAME}.pid #检查日志 service_check(){ if [ -L ${PATH_LOG}/${JAR_NAME}.log ];then rm -f ${PATH_LOG}/${JAR_NAME}.log && \ echo "${PATH_LOG}/${JAR_NAME}.log has been deleted." return 0; fi } #检查服务状态 service_status(){ while read line do ps -ef | grep -e "${line}" | grep -v grep >/dev/null 2>&1 if [ $? -ne 0 ];then echo -e "\033[32m ${line} is running. \033[0m" # rm -rf $PID_PATH_NAME # return 1; else echo -e "\033[31m ${line} is not running. \033[0m" # ps -ef | grep $PATH_TO_JAR | grep -v grep | awk '{print $2}' > $PID_PATH_NAME >/dev/null 2>&1 # return 0; fi done<${PATH_WEBAPPS}/server_pwd.txt } #强制停止服务进程 service_stop_force(){ for i in `ps -ef |grep java | grep "/opt/platform/" |grep pdms | grep -v grep | awk '{print $2}'` do echo $i if [ ! -n $i ];then # rm -rf $PID_PATH_NAME echo -e "\033[32m $i has been killed \033[0m" # return 0; else echo -e "\033[33m $i will be killed by force! \033[0m" kill -9 $i # rm -rf $PID_PATH_NAME # return 0; fi done } #启动所有服务 service_start(){ while read line do nohup $JAVA_HOME/bin/java -jar ${line} >/dev/null 2>&1 & if [ $? -ne 0 ];then echo -e "\033[31m ${line} started failed \033[0m" # exit 1 else # echo $! > $PID_PATH_NAME echo -e "\033[32m ${line} started ... \033[0m" fi sleep 2 # set +e # return 0; done<${PATH_WEBAPPS}/server_pwd.txt } #优雅停止服务 service_stop(){ for p in `ps -ef |grep java | grep "/opt/platform" |grep pdms | grep -v grep | awk '{print $2}'` do if [ ! -n $p ]; then echo -e "\033[33m $p is not running ... \033[0m" # echo "$JAR_NAME stopped ..." # rm -rf $PID_PATH_NAME # return 0; else echo -e "\033[32m $p stoping ... \033[0m" kill $p # return 1; fi done } #更新服务并重启 service_update(){ file_directory=$2 backup=${PATH_WEBAPPS}backup/ datetime=$(date +%Y%m%d) update=/root/update/ if [ -d ${update} ];then for acfile in $file_directory.jar do for acfile1 in "${PATH_WEBAPPS}$file_directory/$file_directory.jar" do diff ${update}$acfile ${acfile1}>/dev/null if [ $? -eq 0 ];then echo -e "\033[31malready new file for $acfile1\033[0m" elif [ -f ${update}$file_directory.jar ];then mkdir ${backup}$datetime cp ${PATH_WEBAPPS}$file_directory/$file_directory.jar ${backup}$datetime cp ${update}$file_directory.jar ${PATH_WEBAPPS}$file_directory/ if [ $? -eq 0 ];then echo -e "\033[32m $acfile update success ${acfile1} \033[0m" return 3 else echo -e "\033[31m $acfile update faild in ${acfile1} \033[0m" fi else echo -e "\033[31m $acfile is not exists... \033[0m" fi # else # "The update directory does not exist" done done fi } #回滚日期显示 service_rollback_list(){ Datetime=$(date +%Y%m%d) echo -e "\033[32m 最近五个可以回滚版本的日期 \033[0m" cd ${PATH_WEBAPPS}backup ls -l |awk '{print $9}' |grep -v "^$" |head -5 } #回滚版本选择并重启 service_rollback_pro(){ echo -e "\033[32m $(date)------------rollback version \033[0m" read -p "请您输入想要回退的版本日期: " DATE Version=$DATE Name=$2 backup_directory=${PATH_WEBAPPS}backup echo -e "\033[41;1m######################### 开始还原到 $Version 版本 #########################\033[0m" if [ ! -n "$Version" ];then echo -e "\033[33m 没有此版本 \033[0m" exit 1 fi if [ -d ${backup_directory}/$Version ];then PID=`ps -ef|grep -v grep |grep $Name|awk '{print $2}'` kill -9 $PID sleep 1 cp $backup_directory/$Version/$Name.jar /opt/platform/$Name/ nohup $JAVA_HOME/bin/java `grep $Name ${PATH_WEBAPPS}server_pwd.txt` >/dev/null 2>&1 & if [ $? -eq 0 ]; then echo -e "\033[32m 已经还原到 $Version 版本 \033[0m" exit 0 else echo -e "\033[31m 还原失败 \033[0m" exit 1 fi fi } #未用 service_application(){ file=$2 echo $file Pid=`ps -ef|grep -v grep |grep -e $file|awk '{print $2}'` echo Pid kill $Pid && echo "`date "+%F %T"` $file killed " sleep 3 ps -ef |grep -v grep |grep $file if [ $? -eq 0 ]; then kill -9 $Pid >/dev/null echo -e "\033[31m $file process is forcibly killed \033[0m" fi nohup $JAVA_HOME/bin/java `grep $file ${PATH_WEBAPPS}/server_pwd.txt` >/dev/null 2>&1 & if [ $? -eq 0 ]; then echo -e "\033[32m start $file ok \033[0m" exit 0 else echo -e "\033[31m start $file error \033[0m" exit 1 fi } service_tag(){ echo -e "\033[32m----------------------------------------------------------------------------------------------------------\033[0m" } service_info(){ service_tag echo "info-use service ${JAR_NAME} start|stop|status|restart" echo "info-jar $PATH_TO_JAR" echo "info-log ${PATH_LOG}/$LOG_NAME.log" # echo "info-gc-log ${PATH_LOG}/${JAR_NAME}_gc.log" # echo "info-heapdump-log ${PATH_LOG}" # echo "info-memory -Xms256m -Xmx2048m" service_tag } # 接收命令开始 case "$1" in "status") service_tag service_status service_info ;; "start") # service_status >/dev/null 2>&1 # service_check service_start service_tag # service_status ;; "stop") # service_status >/dev/null 2>&1 service_tag service_stop sleep 3 service_stop_force ;; "restart") # service_status >/dev/null 2>&1 service_stop sleep 10 service_stop_force # service_check service_start service_tag # service_status ;; "update") service_tag service_update $1 $2 # service_application $1 if [ $? -eq 3 ];then Pid=`ps -ef|grep $2 |grep -v grep |awk '{print $2}'` echo $Pid # kill $Pid && echo "`date "+%F %T"` $2 killed " sleep 3 # ps -ef|grep -v grep |grep $2 |awk '{print $2}' # if [ $? -eq 0 ]; then # kill -9 $Pid >/dev/null # echo -e "\033[31m $2 process is forcibly killed \033[0m" # fi nohup $JAVA_HOME/bin/java `grep $2 ${PATH_WEBAPPS}/server_pwd.txt` >/dev/null 2>&1 & if [ $? -eq 0 ]; then echo -e "\033[32m start $2 ok \033[0m" exit 0 else echo -e "\033[31m start $2 error \033[0m" exit 1 fi else echo -e "\033[31 Applications do not need to be updated \033[0m" fi ;; "rollback") service_rollback_list service_rollback_pro $1 $2 ;; *) echo $JAR_NAME JAR_COMSPid=`ps -ef|grep -v grep |grep -e "${JAR_NAME}"|awk '{print $2}'` echo JAR_COMSPid kill $JAR_COMSPid && echo "`date "+%F %T"` $1 killed " sleep 3 ps -ef |grep -v grep |grep -e "${JAR_NAME}" if [ $? -eq 0 ]; then kill -9 $JAR_COMSPid >/dev/null echo -e "\033[31m ${JAR_NAME} process is forcibly killed \033[0m" fi nohup $JAVA_HOME/bin/java `grep ${JAR_NAME} ${PATH_WEBAPPS}/server_pwd.txt` >/dev/null 2>&1 & if [ $? -eq 0 ]; then echo -e "\033[32m start $1 ok \033[0m" exit 0 else echo -e "\033[31m start $1 error \033[0m" exit 1 fi ;; esac
server_pwd.txt内容
-server -Xms1024m -Xmx1024m -jar 应用目录路径
-server -Xms1024m -Xmx1024m -jar 应用目录路径
-server -Xms1024m -Xmx1024m -jar 应用目录路径
#!/bin/bash APPPATH=/opt/platform/ BASENAME=$(basename $0) echo "$BASENAME" | grep '/' if [ $? -eq 0 ]; then WORKDIR=${BASENAME%/*} else WORKDIR=$(pwd) fi cd $WORKDIR EXEC=$WORKDIR/$BASENAME ## java env export JAVA_HOME=/opt/tools/jdk1.8.0_111 export JRE_HOME=$JAVA_HOME/jre #export JAR_NAME=`grep -w "$1" $APPPATH/server_pwd.txt|awk '{print $NF}'` function stopjava(){ tac $APPPATH/server_pwd.txt >/tmp/jar_pwd_tac02.txt awk -F"/" '{print $NF}' /tmp/jar_pwd_tac02.txt > /tmp/service.txt while read COMS_tacPWD do echo $COMS_tacPWD pidnu=`ps -ef|grep "${COMS_tacPWD}"|grep -v grep|awk '{print $2}'` if [ -n "$pidnu" ];then kill -15 $pidnu sleep 1 else echo "${COMS_tacPWD}未启动" fi count=`ps -ef|grep -v grep |grep -e "${COMS_tacPWD}"|awk '{print $2}' |wc -l` while [ $count -gt 0 ] do sleep 1 count=`ps -ef|grep -v grep |grep -e "${COMS_tacPWD}"|awk '{print $2}' |wc -l` echo $count done echo "=== stop ${COMS_tacPWD} ===success" done</tmp/service.txt # echo "success kill" # if [ -z "$pidnu" ];then # echo "$COMS_tacPWD has stopped" # sleep 1 # continue # fi # kill -9 $pidnu && echo "`date "+%F %T"` ${COMS_tacPWD} killed " # sleep 5 # if [ $? -eq 0 ]; then # echo " stop coms-jar-all ok" # else # echo "stop coms-jar-all error" # fi } function startjava() { tac $APPPATH/server_pwd.txt >/tmp/jar_pwd_tac02.txt awk -F"/" '{print $NF}' /tmp/jar_pwd_tac02.txt > /tmp/service.txt while read COMS_PWD do pidnu=`ps -ef|grep "$COMS_PWD"|grep -v grep|awk '{print $2}'` if [ ! -z "$pidnu" ];then echo "$COMS_PWD has started" sleep 1 continue fi done</tmp/service.txt while read ServerName do nohup $JRE_HOME/bin/java $ServerName >/dev/null 2>&1 & sleep 5 echo "=== start ${ServerName} ===success" done<$APPPATH/server_pwd.txt } function restartjava() { stopjava startjava } case "$1" in stop) stopjava ;; start) startjava ;; restart) restartjava ;; update-all) $EXEC stop sleep 15 $EXEC start echo "=== restart coms-jar-all" ;; *) echo "Now Stoping ==== $JAR_NAME ====" JAR_COMSPid=`ps -ef|grep -v grep |grep -e "${JAR_NAME}"|awk '{print $2}'` echo "Pid Number is === $JAR_COMSPid ===" #kill -9 $JAR_COMSPid && echo " Operation Time is:=== `date "+%F %T"` $1 killed === " kill -15 $JAR_COMSPid sleep 1 count=`ps -ef|grep -v grep |grep -e "${JAR_NAME}"|awk '{print $2}' |wc -l` # count=1 while [ $count -gt 0 ] do sleep 1 count=`ps -ef|grep -v grep |grep -e "${JAR_NAME}"|awk '{print $2}' |wc -l` echo $count done echo "success kill" nohup $JRE_HOME/bin/java `grep ${JAR_NAME} $APPPATH/server_pwd.txt` >/dev/null 2>&1 & if [ $? -eq 0 ]; then echo " Starting ==== $1 OK ====" exit 0 else echo "start $1 error" exit 1 fi ;; esac exit 0
#!/bin/bash ############### ###:@Target: restart pdms-pdloan services ###:Author: Robert Luo ###:DATE : 20170306 ###:Email : robert_luo1949@163.com ###:Deploy env: Centos 6.6 ###:Other: jenkins version : 2.7.2 ############### ####content structure### #@01 define var and functions #@02 get queque of jar packages #@03 shutdown jar packages #@04 check shutdowned status of jar packages #@05 starting jar packages #@06 check started status of jar packages ####content structure### #@01 define var and functions countpid="00" pdms_HE="/opt/platform/data" JRE_HOME="/usr/local/jdk1.8.0_111/jre" JAVA_HOME="/usr/local/jdk1.8.0_111" PATH=$PATH:$HOME/bin export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH #@02 get queque of jar packages cd ${pdms_HE} filelist=$(cat server_pwd.txt) count=`cat server_pwd.txt |grep jar |wc -l` ##count jar packages and assign to variable of count jarfile=`sed -n ${i}p server_pwd.txt` ##get jar package list and assign to variable of jarfile jarshortfile=${jarfile##*-jar} ##get shortage jar package list and assign to variable of jarshortfile ## jarfile=`cat server_pwd.txt |head -n ${i} |tail -n 1 ` tac ${pdms_HE}/server_pwd.txt >>/tmp/jar_pwd_tac.txt ##write jar package list order by esc #@01 define var and functions function Do_kill_ALL () { ##pkill -term java kill -TERM `ps -ef|grep -v grep |grep -e "$1"|awk '{print $2}'` && echo "`date "+%F %T"` $1 killed " countpid=`ps -ef |grep java |grep -v grep |wc -l` echo -e "The sum number of current jars is:\c"& echo {countpid} # if [${countpid} -eq 0];then if [ ${countpid} -eq 0 ] ; then echo "☆☆☆☆☆☆☆☆SHUTDOWN: Shutdowned jar packages successfully!" else echo "☆☆☆☆☆☆☆☆SHUTDOWN: Shutdowned jar packages unsuccessfully!" fi } function Do_kill () { ##pkill -term java #kill -TERM `ps -ef|grep -v grep |grep -e "$1"|awk '{print $2}'` && echo "`date "+%F %T"` $1 killed " ##echo $1 ##jarpid=`ps -ef |grep "$1" |grep -v grep |awk '{print $2}'` ps -ef | grep -v grep | grep $1 | awk '{printf(" kill %s\n",$2)}' | sh echo -e "☆☆☆☆☆☆☆☆KILL: killing: \c"&& echo $1 } function Do_start () { #echo $JRE_HOME #echo $1 # nohup $JRE_HOME/bin/java $1&>/dev/null 2>&1 & nohup $JRE_HOME/bin/java $1&>/dev/null 2>&1 & echo -e "count jar number:\c" echo $2 if [ ${countpid} -eq 1 ] ;then echo "☆☆☆☆☆☆☆☆START: starting $1 jar package." else echo "☆☆☆☆☆☆☆☆START: starting $1 jar package." fi } function Do_check () { countpid=`ps -ef |grep $1 |grep -v grep |wc -l` #echo ${countpid} if [ ${countpid} -eq 1 ] ;then echo "☆☆☆☆☆☆☆☆CHECK: Started $2 jar packages successfully!" else echo "☆☆☆☆☆☆☆☆CHECK: Started $2 jar packages unsuccessfully!" fi } function Do_check_kill () { countpid=`ps -ef |grep $1 |grep -v grep |wc -l` if [ ${countpid} -eq 0 ] ;then echo "☆☆☆☆☆☆☆☆CHECK: kill pd_loan jar packages successfully!" else echo "☆☆☆☆☆☆☆☆CHECK: kill pd_loan jar packages unsuccessfully!" echo "☆☆☆☆☆☆☆☆CHECK: exec command pkill -term java" pkill -term $1 sleep 5s fi } #@03 shutdown pids of jar packages for (( i=1;i<=$count;i++ )) do echo -e "kill jar number :\c" echo ${i} jarfileesc=`sed -n ${i}p /tmp/jar_pwd_tac.txt` jarshortfileesc=${jarfileesc##*-jar} ##echo ${jarshortfileesc} Do_kill ${jarshortfileesc} delaynum=$[${count} - 3] ##define how many delay pid numbers if [ ${i} -le ${delaynum} ];then sleep 1s ###front 3 jars sleep 1 seconds else sleep 3s ###other jars sleep 3 second fi done Do_check_kill "java" #@04 starting pids of jar packages jar packages within for loop for (( i=1;i<=$count;i++ )) do ## jarfile=`cat server_pwd.txt |head -n ${i} |tail -n 1 ` jarfile=`sed -n ${i}p ${pdms_HE}/server_pwd.txt` #@05 starting pids of jar packages jarshortfile=${jarfile##*-jar} Do_start "${jarfile}" ${i} if [ ${i} -le 2 ];then sleep 15s ###front 3 jars sleep 10 seconds else sleep 4s ###other jars sleep 1 second fi #@06 check started status of jar packages Do_check "${jarshortfile}" "${jarfile}" done