Nohup deployment jar using shell scripts to the remote server service on jenkins

First posted, I was the last core configuration jenkins

#停止服务,备份jar
ssh -p 22 [email protected] 'bash -s ' < /opt/auto-deploy-scripts/remove_alk_test_api.sh /opt/jar/ alk-wxapi-test.jar

#copy jar
scp -P22 /mnt/jenkins/workspace/alk_api_test_auto_deploy/target/uberjar/alk-wxapi-test.jar [email protected]:/opt/jar/

#重启服务
ssh -p 22 [email protected] 'bash -s' < /opt/auto-deploy-scripts/start_alk_test_api.sh /opt/jar/ alk-wxapi-test.jar

remove_alk_test_api.sh script:

#!/bin/bash
#获取第一个参数,jar包路径,先备份,再发布
jar_path=$1
#!编译好的jar包名称
jar_name=$2
file=$jar_path$jar_name
bak_file=$jar_name.`date +%Y%m%d%H%M%S`
bak_path="bak/"

if [ $jar_name == ""  ] ;then

echo " jar name is null"
exit 5

else
echo "================== service stop start======================="
pid=`ps -aux | grep $jar_name | grep -v bash |grep -v grep | awk '{print $2}'`
if [ -n "${pid}" ]
then
#!kill -9 强制终止
   echo "kill -9 的pid:" $pid
   kill -9 $pid
fi
echo "================== service stop end ======================="

echo "service stop successed!"
echo "================== jar backup start ======================="

#!将现有的jar备份后,将新的jar包替换
cd $jar_path
echo "file origin is: $file"
echo "backup jar is: $jar_path$bak_path"
if [ -f "$file" ]
then
mv $jar_name $bak_file
cp $bak_file $jar_path$bak_path
fi

echo "================== jar backup end ======================="
fi

start_alk_test_api.sh script reads:

#!/bin/bash
#!启动应用
echo "================== startup start ======================="
#获取第一个参数,jar包路径,先备份,再发布
jar_path=$1
#!编译好的jar包名称
jar_name=$2
file=$jar_path$jar_name

#远程执行该脚本时改变了环境变量,找不到java的环境变量,提示:nohup: failed to run command ‘java’: No such file or directory
#所以需要加上以下java的环境变量,其中JAVA_HOME的值,可以通过执行命令echo $JAVA_HOME得到,每个人的java环境变量设置不一样
#该问题参考自:https://stackoverflow.com/questions/20791846/zookeeper-not-starting-nohup-error
export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export PATH=$JAVA_HOME/bin:$PATH

echo "授予当前用户权限"
#chmod 777 $file
echo "执行....."
cd $jar_path
nohup java -jar $file > ${jar_path}/log/nohup.log 2>&1 &
echo "启动jar的命令: nohup java -jar $file > ${jar_path}log/nohup.log 2>&1 & "
echo "================== startup end ======================="

remove_alk_test_api.shAnd start_alk_test_api.shon jenkins server, you need these two distal shell in 0.0.0.0 (not exposed) to perform the
right, Keguan glance, you see it, feel it simply is not worth mentioning, first, do not worry, to search for the problem of the chicken dishes and there is also much to listen to me 11 years.

1, the first question: ps -ef | grep xxx.jar found in excess pid
phenomenon: in 0.0.0.0 uplink ps -ef | grep alk-wxapi-test.jar | grep -v grep | awk '{print $2}'can only be found in a pid, but these words into the shell script, use the command locally remote execution, will find 3 pid, but failed when an error kill.
Now we know why, but there was indeed a problem.
It has been resolved inconvenient to reproduce, and therefore description on the line.
The reason: shell in this command is executed remotely via a bash, so else found irrelevant the fact pid bash, grep is therefore in addition to exclude itself grep ( grep -v grep) also exclude foreign bash ( grep -v bash)
The last command is:

pid=`ps -aux | grep $jar_name | grep -v bash |grep -v grep | awk '{print $2}'`

2, remote script execution, which has nohup java -jar, an error occurs: nohup: failed to run command ‘java’: No such file or directory
This is because the nohup java environment variable is not read, approach is to be added before the nohup execution

export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export PATH=$JAVA_HOME/bin:$PATH

3. Other ideas

  • The jar of service configuration to the server a service, use systemctl stop, start, and so on.
  • Use docker Deployment Services.

Both are also good ideas, and doubtless have the opportunity to experience herein, this this.

reference:

ps -ef | grep -v seemingly ineffective in shell sh script?
zookeeper cluster structures encountered in the pit under linux
nohup: can not run command abnormalities resolve
Zookeeper not starting, nohup error

Reproduced in: https: //www.jianshu.com/p/34708c0fde16

Guess you like

Origin blog.csdn.net/weixin_34289744/article/details/91189854