#!/bin/bash

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

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

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

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

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

rm -f ~/run/shops-timers.pid