问题:
jenkins的shell中执行nohup java -jar xxx.jar &
,构建成功后在服务器上通过jps、netstat、ps等命令发现均无法查到进程
原因:
- 没有合理设置环境变量
BUILD_ID
、JENKINS_NODE_COOKIE
- 执行
nohup java -jar xxx.jar &
命令时已经离开了该 job 的WORKSPACE - ssh管道到其他node执行
nohup java -jar xxx.jar &
时环境变量缺失
方案:
#!/bin/bash
export MAVEN_HOME=/home/maven/apache-maven-3.8.3
export PATH=${MAVEN_HOME}/bin:${PATH}
export JAVA_HOME=/home/java/jdk1.8.0_202
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=${JAVA_PATH}:${PATH}
export BUILD_ID=dontKillMe
pkg_name="com-0.0.1-SNAPSHOT.jar"
share_dir="${WORKSPACE}/target/"
port="8084"
log_file="nohup.out"
log_file_archived="nohup_archived.out"
function _get_pid(){
app_pid="`netstat -anpt | grep "0.0.0.0:${port}" | awk '{print $7}' | awk -F'/' '{print $1}'`"
echo $app_pid
}
function _logs_status(){
for((i=1;i<=30;i++));
do
sleep ${i}s
run_status="`egrep 'Started ComApplication in' ${share_dir}${log_file} | wc -l`"
if [ ${run_status} == 1 ];then
tail -50 ${share_dir}${log_file}
app_pid="`_get_pid`"
echo "====== server start success ======"
echo "app_pid: ${app_pid}"
break
fi
done
if [ ${run_status} == 0 ];then
echo "====== server start failed ======"
exit 128
fi
}
function _stop(){
app_pid="`_get_pid`"
echo "app_pid: ${app_pid}"
if [ -z "${app_pid}" ];then
echo "====== server is stoped ======"
else
kill -9 ${app_pid}
sleep 10s
app_pid="`_get_pid`"
if [[ -z "${app_pid}" ]];then
echo "====== server stoped success ======"
else
echo "====== server stoped failed ======"
exit 127
fi
fi
}
function _start(){
# cp -rf ${WORKSPACE}/target/${pkg_name} ${share_dir}
cd ${share_dir} && \
chmod 777 ${pkg_name} && \
tail -200 ${share_dir}${log_file} >> ${share_dir}${log_file_archived} && \
rm -rf ${share_dir}${log_file}
export JENKINS_NODE_COOKIE=dontKillMe
export BUILD_ID=dontKillMe
nohup java -jar ${share_dir}/${pkg_name} &>${share_dir}${log_file} &
sleep 5s
_logs_status
}
function _restart(){
_stop
_start
}
function _main(){
_restart
}
_main