代码之家  ›  专栏  ›  技术社区  ›  Damian

PID自动递增

  •  0
  • Damian  · 技术社区  · 9 年前

    我正在为一个微服务编写一个初始化脚本,但有一个问题,程序(通过echo)发出的进程的PID不是进程所具有的进程ID。代码:

    #!/bin/bash
    
    ### BEGIN INIT INFO
    # Provides: temp
    # Description: temp
    # required-start: $local_fs $remote_fs $network $syslog
    # required-stop: $local_fs $remote_fs $network $syslog
    # default-start: 3 5
    # default-stop: 0 1 2 6
    # chkconfig: 35 99 1
    # description: Microservice init-script
    ### END INIT INFO
    
    START_SCRIPT=${applicationDirectory}/script/start.sh
    STOP_SCRIPT=${applicationDirectory}/script/stop.sh
    PID_FILE=${runDirectory}/${microserviceName}_${environment}_${servicePort}
    
    # ***********************************************
    # ***********************************************
    
    DAEMON=$START_SCRIPT
    
    # colors
    red='\e[0;31m'
    green='\e[0;32m'
    yellow='\e[0;33m'
    reset='\e[0m'
    
    echoRed() { echo -e "${red}$1${reset}"; }
    echoGreen() { echo -e "${green}$1${reset}"; }
    echoYellow() { echo -e "${yellow}$1${reset}"; }
    
    start() {
      #PID=`bash ${START_SCRIPT} > /dev/null 2>&1 & echo $!`
      PID=`$DAEMON $ARGS > /dev/null 2>&1 & echo $!`
    }
    
    stop() {
        STOP_SCRIPT $1
    }
    
    case "$1" in
    start)
        if [ -f $PID_FILE ]; then
            PID=`cat $PID_FILE`
            if [ -z "`echo kill -0 ${PID}`" ]; then
                echoYellow "Microservice is already running [$PID]."
                exit 1
            else
                rm -f $PID_FILE
            fi
        fi
    
        start
    
        if [ -z $PID ]; then
            echoRed "Failed starting microservice."
            exit 3
        else
            echo $PID > $PID_FILE
            echoGreen "Microservice successfully started [$PID]."
            exit 0
        fi
    ;;
    
    status)
        if [ -f $PID_FILE ]; then
            PID=`cat $PID_FILE`
            if [ ! -z "`echo kill -0 ${PID}`" ]; then
                echoRed "Microservice is not running (process dead but pidfile exists)."
                exit 1
            else
                echoGreen "Microservice is running [$PID]."
                exit 0
            fi
        else
            echoRed "Microservice is not running."
            exit 3
        fi
    ;;
    
    stop)
        if [ -f $PID_FILE ]; then
            PID=`cat $PID_FILE`
            if [ ! -z "`echo kill -0 ${PID}`" ]; then
                echoRed "Microservice is not running (process dead but pidfile exists)."
                exit 1
            else
                PID=`cat $PID_FILE`
                stop $PID
                echoGreen "Microservice successfully stopped [$PID]."
                rm -f $PID_FILE
                exit 0
            fi
        else
            echoRed "Microservice is not running (pid not found)."
            exit 3
        fi
    ;;
    
    *)
        echo "Usage: $0 {status|start|stop}"
        exit 1
    esac
    

    现在,程序给出例如2505作为PID。但当我使用

    ps aux | grep trans | grep -v grep
    

    它输出一个数字,即先前输出的数字+1。

    有人能猜一下吗?感谢任何帮助!

    1 回复  |  直到 9 年前
        1
  •  0
  •   Cong Ma    9 年前

    你的 PID 变量获取执行的shell的pid start.sh 。脚本执行的实际程序获得不同的pid。