jar包启停
建议以下脚本和部署的jar(服务)放在同一目录下。
start.sh
jar启动脚本,默认会查找当前目录下jar进行启动,也可传入对应需启动jar路径。
#! /bin/bash
#进入脚本所在目录
cd `dirname $0`
#不传需要启动的jar,默认在当前目录下查找
server_name=$1
if [ -z "$server_name" ];then
server_name=$(find . -name '*.jar'|head -1)
if [[ $server_name =~ "/" ]];then
server_name=${server_name##*/}
fi
fi
#如果要启动的jar非普通文件,直接返回错误
if [ ! -f "$server_name" ];then
echo "not find server -> ${server_name}"
exit 1
fi
echo "start ${server_name}"
#export JAVA_HOME=jre地址
#export PATH=$PATH:${JAVA_HOME}/bin
#执行
nohup java -XX:+UseG1GC -Xmx512m -XX:MaxGCPauseMillis=200 -XX:GCPauseIntervalMillis=300 -jar $server_name --spring.profiles.active=prod > /dev/null 2>&1 &
exit 0
不能正常启动大部分原因可能是java环境变量问题。
stop.sh
jar停止脚本,默认优雅地停止。可设置默认超时秒数(默认10秒),如果超过超时秒数仍未成功停止,则直接kill进程。
#! /bin/bash
#进入脚本所在目录
cd `dirname $0`
server_name=$1
#默认优雅停止,超过指定秒数直接kill
timeout_seconds=$2
#不传需要停止的服务名,默认在当前目录下查找
if [ -z "$server_name" ];then
server_name=$(find . -name '*.jar'|head -1)
if [[ $server_name =~ "/" ]];then
server_name=${server_name##*/}
fi
fi
#如果要停止的jar非普通文件,直接返回成功
if [ ! -f "$server_name" ];then
echo "not find server -> ${server_name}"
exit 0
fi
#默认超时时间10秒
default_seconds=10
if [ -z "$timeout_seconds" ];then
timeout_seconds=$default_seconds
fi
expr $timeout_seconds + 1 > /dev/null 2>&1
timeout_seconds=$([ $? -ne 0 ] && echo $default_seconds || echo ${timeout_seconds})
server_pid=`ps -ef|grep java|grep $server_name|awk '{print $2}'`
echo "kill ${server_pid} -> ${server_name}"
if [ -n "$server_pid" ]; then
kill ${server_pid}
tmp_pid=${server_pid}
while [ -n "$tmp_pid" ]
do
sleep 1
tmp_pid=`ps -ef|grep java|grep $server_name|awk '{print $2}'`
timeout_seconds=`expr $timeout_seconds - 1`
if [ $timeout_seconds -lt 0 ] && [ -n "$tmp_pid" ];then
echo "kill -9 ${server_pid} -> ${server_name}"
kill -9 ${tmp_pid}
break;
fi
done
fi
exit 0
$1
:服务名称,不写会默认查找
$2
:停止超时时间,默认10。超过默认时间走kill -9
bak.sh
备份脚本,默认备份当前目录下查找到的一个jar,也可传入jar目录。
#! /bin/bash
#进入脚本所在目录
cd `dirname $0`
day_ymd=$(date +'%Y%m%d')
server_name=$1
if [ -z "$server_name" ];then
server_name=$(find . -name '*.jar'|head -1)
if [[ $server_name =~ "/" ]];then
server_name=${server_name##*/}
fi
fi
if [ ! -f "$server_name" ];then
echo "not find server -> ${server_name}"
exit 0
fi
suffix_numb=0
for line in $(find ./ -name "${server_name}.${day_ymd}*")
do
tmp_numb=${line##*-}
if [ -n "$tmp_numb" ]; then
expr $tmp_numb + 1 > /dev/null 2>&1
tmp_numb=$([ $? -ne 0 ] && echo 0 || echo ${tmp_numb})
if [ $suffix_numb -lt $tmp_numb ]; then
suffix_numb=$tmp_numb
fi
fi
done
suffix_numb=$(expr ${suffix_numb} + 1)
mv ${server_name} ${server_name}.${day_ymd}-${suffix_numb}
exit 0
默认的备份格式是:${server_name}.yyyyMMdd-数字
remote_ci.sh
远程部署脚本,功能:停止远程服务->备份远程服务->传送当前jar到远程服务器->启动远程服务
#! /bin/bash
#进入脚本所在目录
server_path=$(cd `dirname $0`; pwd)
cd $server_path
remote_address=root@ip
passwd=服务器密码
#远程jar所在目录,这里默认为$server_path
remote_server_path=$server_path
timeout=20
passwderror=0
#需要scp传送的jar名称
server_name=$1
if [ -z "$server_name" ];then
server_name=$(find . -name '*.jar'|head -1)
if [[ $server_name =~ "/" ]];then
server_name=${server_name##*/}
fi
fi
if [ ! -f "$server_name" ];then
echo "not find server -> ${server_name}"
exit 0
fi
#停止远程服务,并备份
/usr/bin/expect <<EOF
spawn ssh $remote_address "cd ${remote_server_path} ; sh ./stop.sh; sh ./bak.sh"
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "passwd is error"
exit 2
}
set timeout 1000
set passwderror 1
send "$passwd\r"
exp_continue
}
"*es/no)?*" {
send "yes\r"
exp_continue
}
timeout {
puts "connect is timeout"
exit 3
}
}
EOF
#把新构建的jar传到远程服务器
/usr/bin/expect <<EOF
spawn scp ${server_name} ${remote_address}:${remote_server_path}
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "passwd is error"
exit 2
}
set timeout 1000
set passwderror 1
send "$passwd\r"
exp_continue
}
"*es/no)?*" {
send "yes\r"
exp_continue
}
timeout {
puts "connect is timeout"
exit 3
}
}
EOF
#启动远程服务器上的jar
/usr/bin/expect <<EOF
spawn ssh $remote_address "cd ${remote_server_path} ; sh ./start.sh"
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "passwd is error"
exit 2
}
set timeout 1000
set passwderror 1
send "$passwd\r"
exp_continue
}
"*es/no)?*" {
send "yes\r"
exp_continue
}
timeout {
puts "connect is timeout"
exit 3
}
}
EOF
exit 0
expect需要提前安装,
yum install expect
remote_ci.sh 优化版
远程部署脚本,功能:传送当前jar到远程服务器->停止远程服务->备份远程服务->启动远程服务
#! /bin/bash
#进入脚本所在目录
server_path=$(cd `dirname $0`; pwd)
cd $server_path
remote_address=root@ip
passwd=服务器密码
#远程jar所在目录,这里默认为$server_path
remote_server_path=$server_path
#临时目录($server_path下),需要提前创建
remote_server_temp_path=temp
timeout=20
passwderror=0
server_name=$1
if [ -z "$server_name" ];then
server_name=$(find . -name '*.jar'|head -1)
if [[ $server_name =~ "/" ]];then
server_name=${server_name##*/}
fi
fi
if [ ! -f "$server_name" ];then
echo "not find server -> ${server_name}"
exit 0
fi
/usr/bin/expect <<EOF
spawn scp ${server_name} ${remote_address}:${remote_server_path}/${remote_server_temp_path}
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "passwd is error"
exit 2
}
set timeout 1000
set passwderror 1
send "$passwd\r"
exp_continue
}
"*es/no)?*" {
send "yes\r"
exp_continue
}
timeout {
puts "connect is timeout"
exit 3
}
}
EOF
/usr/bin/expect <<EOF
spawn ssh $remote_address "cd ${remote_server_path} ; sh ./stop.sh; sh ./bak.sh; mv -f ./${remote_server_temp_path}/${server_name} ./; sh ./start.sh"
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "passwd is error"
exit 2
}
set timeout 1000
set passwderror 1
send "$passwd\r"
exp_continue
}
"*es/no)?*" {
send "yes\r"
exp_continue
}
timeout {
puts "connect is timeout"
exit 3
}
}
EOF
exit 0