#!/bin/bash

if [ ! -f ~/run/adscounter-temp-scheduler.pid ]; then
  echo "Server [adscounter-temp-scheduler] doest not started!"
  exit
fi

pid=`cat ~/run/adscounter-temp-scheduler.pid`
if [ ! -n "${pid}" ]; then
  echo "illegal content in adscounter-temp-scheduler.pid!"
  exit
fi

process=`ps -aux|awk '{print $2}' | grep ${pid}`
if [ -n "${process}" ]; then
  echo "Server [adscounter-temp-scheduler] will be stop."
  kill ${pid}

  times=0
  while true
  do
    process=`ps -aux|awk '{print $2}' | grep ${pid}`
    if [ -n "${process}" ] && [ ${times} -lt 60 ]; then
      let times++
      sleep 1
    else
      break
    fi
  done

  process=`ps -aux|awk '{print $2}' | grep ${pid}`
  if [ -n "${process}" ]; then
    echo "Server [adscounter-temp-scheduler] cannot stop, Please check pid file 'adscounter-temp-scheduler.pid' and stop it by manually!"
    exit
  fi
else
  echo "Server [adscounter-temp-scheduler] has been stoped!"
fi

rm -f ~/run/adscounter-temp-scheduler.pid