阅读 73

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即可

原文:https://www.cnblogs.com/reblue520/p/15136440.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐