1. 程式人生 > 其它 >jenkins使用ssh remote外掛執行shell後無法退出的問題處理

jenkins使用ssh remote外掛執行shell後無法退出的問題處理

指令碼

#!/bin/bash

# 根據引數,執行程序的啟動 停止 重啟等
#source /etc/profile

# 非apache使用者執行指令碼,則退出
if [ `whoami` != "apache" ];then
echo " only apache can run me"
exit 1
fi


process='bin/geo'

##############$process###############
#1.啟動 $process
start_geo() {
    sleep 1
    pid=`ps -ef |grep $process |grep -v grep |awk
'{print $2}'` if [ ! "$pid" ];then echo "Starting $process process is $process;pid is $pid " if [ $? -ne 0 ]; then echo exit 1 fi cd /data/www/vhosts/test-api-location.aa.me/httpdocs/geo && nohup /data/www/vhosts/test-api-location.aa.me/httpdocs/geo/$process --config=config/uat_config.toml >/dev/null
2>&1 & sleep 2 swoft_nu=`netstat -tnlp|grep 20109|wc -l` if [[ $? == 0 && ${swoft_nu} -ge 1 ]];then echo "start $process $process ok" else echo "start $process $process failed" fi else echo "$process $process is still running!
" exit fi } #2.停止 $process stop_geo() { echo -n $"Stopping $process $process: " pid=`ps -ef |grep $process |grep -v grep |awk '{print $2}'` if [ ! "$pid" ];then echo "$process $process is not running" else ps -ef|grep ${process}|grep -v grep|awk '{print $2}'|xargs kill -9 sleep 2 swoft_nu=`netstat -tnlp|grep 20109|wc -l` if [[ ${swoft_nu} -lt 1 ]];then echo "stop $process $process ok killed $pid" fi fi } #3.重啟 restart_geo restart_geo() { stop_geo start_geo } #4.檢視 $process 狀態 status_geo(){ pid=`ps -ef |grep $process |grep -v grep |awk '{print $2}'` if [ ! "$pid" ];then echo "geo $process is not running" else echo "geo $process is running" fi } ##################### MAIN ############################### usage () { echo "" echo " Please Input server infomation!" echo "" echo " USAGE: `basename $0` [start|stop|restart|status]" echo "" } if [ $# != 1 ] then usage >&2 exit 1 fi OPT=$1 case $OPT in start) echo "start `basename $0`" start_geo ;; stop) stop_geo ;; restart) restart_geo exit 0 ;; status) status_geo ;; *) echo "Usage:`basename $0` [start|stop|restart|status]" exit 1 esac

網上有說加 exit 0,或者去掉 source /etc/profile,加nohup或者sleep 1都無效,是因為沒有使用終端無法正常退出,否則程式會終止,勾選pty即可