1.打包
与平常maven项目打包一致,不再赘述
2.杀死原有进程
通过
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
获取当前进程的pid
然后通过kill -9杀死
3.脚本执行java -jar xxx.jar
脚本中执行此命令,在系统中发现并没有运行。
但是将所有命令放这sh脚本中执行,没有任何问题(到Jenkins的workspace下直接sh执行)
经查询资料发现,Jenkins在执行完命令后,会杀死所有的子进程。
所以,需要在运行java -jar前添加一句
BUILD_ID=DONTKILLME
防止Jenkins杀死执行java -jar的子进程
4.完整脚本
#!/bin/sh mvn package APP_NAME=jiramail.jar path=`pwd` pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` if [ -n "${pid}" ]; then kill -9 $pid fi #cp ./target/jiramail-0.0.1-SNAPSHOT.jar /home/yuhaokui/jiramail.jar mv ${path}/target/jiramail-0.0.1-SNAPSHOT.jar ${path}/target/jiramail.jar BUILD_ID=DONTKILLME java -jar ${path}/target/jiramail.jar --server.port=9000 &