java程序启动重启脚本

#!/bin/bash
cmd=$1
args='-Xms2g -Xmx2g'
if [ $# -gt  2 ] ; then
 echo "$#"
 app='$2'
   echo "Two parameters $app"
 else
  echo "$#"
    app='$2'
  echo "Three parameters $app"
fi

pid=`ps -ef|grep java|grep $app|awk '{print $2}'`
 echo $pid

startup(){
 java -jar $args /java/$app &
}

if [ ! $cmd ]; then
  echo "Please specify args 'start|restart|stop'"
  exit
fi

if [ $cmd == 'start' ]; then
  if [ ! $pid ]; then
    startup
  else
    echo "$app is running! pid=$pid"
  fi
fi

if [ $cmd == 'restart' ]; then
  if [ $pid ]
    then
      echo "$pid will be killed after 3 seconds!"
      sleep 3
      kill -9 $pid
  fi
  startup
fi

if [ $cmd == 'stop' ]; then
  if [ $pid ]; then
    echo "$pid will be killed after 3 seconds!"
    sleep 3
    kill -9 $pid
  fi
  echo "$app is stopped"
fi