jenkins使用ssh remote插件执行shell后无法退出的问题处理

现象:通过jenkins发布代码后,执行远程命令,一直卡在执行远程命令那里打转,无法退出

脚本

复制代码
#!/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即可

posted @   reblue520  阅读(835)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示