PID 自动递增

PID increments automatically

我正在为微服务编写初始化脚本,但遇到问题,程序给出(通过回显)的进程 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}${reset}"; }
echoGreen() { echo -e "${green}${reset}"; }
echoYellow() { echo -e "${yellow}${reset}"; }

start() {
  #PID=`bash ${START_SCRIPT} > /dev/null 2>&1 & echo $!`
  PID=`$DAEMON $ARGS > /dev/null 2>&1 & echo $!`
}

stop() {
    STOP_SCRIPT 
}

case "" 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: [=10=] {status|start|stop}"
    exit 1
esac

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

ps aux | grep trans | grep -v grep

它输出一个数,就是之前输出的数+1。

谁能猜猜看?感谢您的帮助!

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