#!/bin/bash
cd /home/mfqy/card/
serverID=$1
env=zwonline
routeHost=$2
routePort=$3

cwd=`pwd`
echo $cwd

logicMID=$((300000 + serverID))
gatewayMID=$((400000 + serverID))
m1=logic
m2=gateway

m1ret=`ps -ef |pgrep -f "$m1/$m1.*-i $logicMID"`
m2ret=`ps -ef |pgrep -f "$m2/$m2.*-i $gatewayMID"`
#echo "logic logicID=$logicMID id=$m1ret for server=$serverID"


if [ -n "$m1ret" ]; then
    #echo "logic id=$m1ret for server=$serverID"
    echo "$m1ret" > /home/pid/$logicMID.pid
fi

if [ -n "$m2ret" ]; then
    #echo "gateway id=$m2ret for server=$serverID"
    echo "$m2ret" > /home/pid/$gatewayMID.pid
fi


# 检查 m1 进程是否存在
if [ -z "$m1ret" ]; then
    echo "logicnil"
    # 如果 m1 不存在，检查并 kill m2
    if [ -n "$m2ret" ]; then
        kill -9 $m2ret
        echo "Killed gateway process: $m2ret"
    fi
	  nohup $m1/$m1 -i $logicMID -e $env -h $routeHost -p $routePort -log_dir=log -alsologtostderr 2>&1 &
	  sleep 30
    nohup $m2/$m2 -i $gatewayMID -e $env -h $routeHost -p $routePort -log_dir=log -alsologtostderr 2>&1 &
elif [ -z "$m2ret" ]; then
# 检查 m2 进程是否存在
    echo "gatewaynil"
	  nohup $m2/$m2 -i $gatewayMID -e $env -h $routeHost -p $routePort -log_dir=log -alsologtostderr 2>&1 &
fi
