shell脚本-更新进程

#!/bin/bash
#脚本执行格式 sh tesy.sh $1 $2
if [[ $# -ne 2 ]];then
cat << EOF
ERROR :脚本执行格式 sh tesy.sh jar名称 time分秒
	注意: 当前版本shell脚本使用前提是进程已存在
EOF
exit 1
fi

project=$(echo $1 |awk -F "." '{print $1}')
FileDir=`ps -ef|grep $1|egrep -v "grep|sh"|awk '{print $NF}'|awk -F'/' '{gsub("/"$NF,"");print}'`
pid=`ps -ef|grep $1|egrep -v "grep|sh"|awk '{print $2}'`

ps -ef|grep $1|egrep -v "grep|sh"
if [ $? -ne 0 ];then
	cat << EOF
	ERROR $1 不存在!!!
EOF
	exit 1
fi

function DeleteFile(){
	ReservedNum=5
	date=$(date "+%Y%m%d-%H%M%S")
	FileNum=$(ls -l $FileDir|grep $1 |wc -l)
	while(( $FileNum > $ReservedNum))
	do
	    OldFile=$(ls -rt $FileDir|grep $1| head -1)
	    echo  $date "Delete File:"$OldFile
	    cd $FileDir
	    rm -rf $OldFile
	    let "FileNum--"
	done
}

cd $FileDir

echo "1.备份:"
ls -ld $FileDir/$1
cp -rp $FileDir/$1 $FileDir/$1_`date +%Y%m%d`_$2
ls -ld $FileDir/$1_`date +%Y%m%d`_$2

echo "2.更新:"
scp root@10.10.3.107:/home/test/$1 $FileDir/$1
#cp /home/test/$1 $FileDir/$1
chown -R root:root $FileDir/$1
ls -ld $FileDir/$1

echo "3.杀掉进程:"
echo $pid
kill -9 $pid

ps -ef|grep $1|egrep -v "grep|sh"
if [ $? -ne 0 ];then
	echo "4.启动"
	sleep 2
	nohup java -jar -Xms1024m -Xmx1024m $FileDir/$1 > $FileDir/$project.out 2>&1 &
	echo "`date +%Y_%m_%d_%H_%M` : "$1" is restart" >> restart_log
	ps -ef|grep $1|egrep -v "grep|sh"
	echo "tail -f $FileDir/$project.out"

else
	echo "杀死进程失败!!!"
	exit 1
fi

ps -ef|grep $1|egrep -v "grep|sh"
if [ $? -ne 0 ];then
	echo "启动失败!!!"
	exit 1
fi

echo "5.清理备份"
DeleteFile $1
posted @ 2020-01-13 17:43  薄荷少年郎微微凉  阅读(209)  评论(0编辑  收藏  举报