export JAVA_HOME=/usr/local/jdk1.8.0_191
export PATH=$JAVA_HOME/bin:$PATH
export GRADLE_HOME=/usr/local/gradle-4.6
export PATH=$GRADLE_HOME/bin:$PATH
PID=$(ps -ef|grep helloworld |grep -v grep | awk {'print $2'})
echo $PID
if [ -n "$PID" ]; then
kill -9 $PID
fi
gradle build -p /var/lib/jenkins/workspace/helloworld
nohup java -jar /var/lib/jenkins/workspace/helloworld/build/libs/helloworld-0.0.1.jar > /var/lib/jenkins/workspace/output.txt&
------------------------------------------------------------------
在jenkins里面直接执行命令,或者直接执行shell script :
- 找到当前执行的pid
- 找pid: ps -ef | grep *
- 取出当前grep pid : grep -v grep
- 提取pid awk {'print $2'}
- 赋变量:PID=$(ps -ef |grep ** |grep -v grep |awk {'print $2'})
- 判断当前pid,不为空,则杀死进程
- 重新build
- nohup 启动
注意:
- 指定JAVA_HOME,GRADLE_HOME,不然执行java,gradle命令时,会找不到命令
- 查询pid,指定尽量具体的程序,如果找到多个pid,不光不能正常运行,而且jenkins也会被玩死
- nohup运行程序,把日志打印到指定位置,不然nohup不起作用,程序会在jenkins中窗口运行 > /log.log
参考(感谢):
- 存储pid到某个位置,然后去找 https://www.cnblogs.com/vipsoft/p/5252306.html
- 找到pid,然后杀死 https://stackoverflow.com/questions/37738308/using-ps-and-awk-to-get-pid-then-killing-it
- linux判断字符串 if -n https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/