PID自动增加

时间:2015-09-03 09:31:08

标签: daemon pid

我正在为微服务编写一个init脚本并且遇到问题,程序发出的进程的PID(通过echo)不是进程所拥有的进程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 个答案:

答案 0 :(得分:0)

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