Linux自动打包上传并运行SpringBoot程序

SSH 免密登录

生成公钥、私钥

ssh-keygen

发送公钥

ssh-copy-id -i /Users/rick/.ssh/id_rsa.pub root@172.31.1.2

配置别名

.ssh/config

Host xhope
  HostName 172.31.1.2
  User root
  IdentityFile ~/.ssh/id_rsa

Host dao
  HostName 172.31.1.3
  User root
  IdentityFile ~/.ssh/id_rsa

登录

ssh xhope

程序执行脚本

#!/bin/bash

cd /usr/local/projects/dao
PID=`ps -ef | pgrep -f "8082"`
if [  -n "$PID" ]
then
        kill -9 $PID
fi

rm -f product-manager-1.0-SNAPSHOT.jar
cp deploy/product-manager-1.0-SNAPSHOT.jar .

nohup /usr/local/jdk1.8.0_65/bin/java -Xms1024m -Xmx2048m -Dspring.profiles.active=prod -jar product-manager-1.0-SNAPSHOT.jar --server.port=8082 &

exit

mvn.sh

#!/bin/bash

mvn clean package -Dmaven.test.skip=true
scp /Users/rick/Space/Workspace/product-manager/target/product-manager-1.0-SNAPSHOT.jar root@172.31.1.2:/usr/local/projects/dao/deploy

ssh dao "bash -s < /usr/local/projects/dao/clean.sh"

执行 mvn.sh 后,自动打包上传并运行程序