服务启动shell脚本

#!/bin/sh
JarDir=`pwd`
do_start()
{
  echo "pandora-login start ..."
  nohup java -jar -Xmn256m -Xmx2048m -XX:MaxTenuringThreshold=2 -Dspring.profiles.active=ali $JarDir/pandora-login.jar  >/dev/null 2>&1 &
  sleep 3
  SERVER_PID=`ps -ef|grep -v grep| grep "pandora-login.jar"|awk {print $2}`
  if [ "$SERVER_PID" == "" ]; then
    echo "pandora-login start fail"
    return 1
  fi
  i=0
  while [ $i -lt 60 ]
  do
    sleep 1
    cpu_float1=`top -b -n 1 -p $SERVER_PID  2>&1 | awk -v pid=$SERVER_PID {if ($1 == pid)print $9}`
    cpu1=`echo ${cpu_float1%.*}`
    sleep 1
    cpu_float2=`top -b -n 1 -p $SERVER_PID  2>&1 | awk -v pid=$SERVER_PID {if ($1 == pid)print $9}`
    cpu2=`echo ${cpu_float2%.*}`
    port=`netstat -nap | grep 9092 | grep LISTEN | grep $SERVER_PID`
    if [ $cpu1 -lt 150 ] && [ $cpu2 -lt 150 ] && [ "$port" != "" ]; then
      echo "pandora-login start success "$SERVER_PID
      return 0
    fi
    echo "pandora-login starting... "$i
    let i++
  done
  echo "pandora-login start fail"
  return 1
}

do_stop()
{
  SERVER_PID=`ps -ef|grep -v grep| grep "pandora-login.jar"|awk {print $2}`
  echo "stop pandora-login_pid "$SERVER_PID
  for PID in $SERVER_PID
  do
        kill $PID
  done
  while true
  do
    sleep 1
    SERVER_PID_KILL=`ps -ef|grep -v grep| grep "pandora-login.jar"|awk {print $2}`
    if [ "$SERVER_PID_KILL" == "" ]; then
      break
    fi
  done
}

case "$1" in
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart)
        do_stop
        sleep 1
        do_start
        ;;
    *)
esac

 

服务启动shell脚本

上一篇:《linux就该这么学》第五课


下一篇:冬奥福利来袭,联通冬奥主题列车引发围观潮