#!/bin/bash

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

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

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

rm -f ~/run/shopping-web.pid