Shell脚本施行和停止java服务

Shell脚本执行和停止java服务
启动
#!/bin/sh
#set -x

config_filepath=
tmp=
log_filepath=run.log
jar_path=

function usage(){
   echo "Usage: $0 -f filepath"
   echo "filepath is the server config file."
}

function start(){
   if [ $# != 2 ]; then
      echo "Input error. Please input $0 -f filepath"
      exit 1
   fi
  
   if [ ! -f "$2" ]; then
      echo "config file $2 does not exist"
      exit 1
   fi

   config_filepath=$2

   tmp=`ps -ef | grep java| grep "com.xxx.analysis.ServerStartup"|grep $config_filepath | wc -l` 
   if [ $tmp -eq 1 ]; then
      if [ ! -f "$log_filepath" ]; then
         touch $log_filepath
      fi
      echo "analysis already started..."
      exit 1
   fi
  
   echo "analysis is  starting..."

   for i in ./../lib/*.jar
   do
      jar_path="$i:$jar_path"
   done
   echo $jar_path >"$log_filepath" &
   java -cp "$jar_path" com.xxx.analysis.ServerStartup $1 $2 > "$log_filepath" &
   appName=`grep "appName" $2 | sed -e 's/.*=//'`
   echo "$!" > "$appName".pid
   echo "analysis start sucessful..."
   echo "$appName pid is `cat $appName.pid`"
  
}

case $1 in
   -f)
      start $@
   ;;
   *)
      usage
    ;;
esac

exit $?




停止

#!/bin/sh
#set -x

function usage(){
   echo "Usage: $0 appName.pid"
   echo "pid is the appName."
}

function stop(){
   if [ $# != 1 ]; then
      echo "Input error. Please input $0 appName.pid"
      exit 1
   fi

   if [ ! -f "$1" ]; then
      echo "pid file $1 does not exist"
      exit 1
   fi
   kill -9 `cat $1`
   rm -rf $1
}

stop $@

exit $?