scvmm 发表于 2018-8-24 06:16:48

shell 脚本调用java进程

  #!/bin/sh
  #Author: cym
  #2014-05-21
  #----------------How to use----------------#
  #copy make_java.sh to /bin
  #chmod +x make_java.sh
  #monitorAgent_java.sh {start,stop,encode} mainfilename
  #eg:make_java.sh start test.java
  #------------------------------------------#
  #JAVA_BIN=/home/admin/jdk1.7.0_55/bin/java
  JAVA_BIN=/usr/local/jdk1.7.0_60/bin/java
  #MAINFILE=$2
  #MAIN_CLASS="com.cmsz.hostMonitor.rabbitMq.send.HostMonitorDatasMain"
  MAIN_CLASS="com.cmsz.monitor.agent.CollectAgentMain"
  path=$(dirname $0)
  APP_ID=$path"/OMCollectAgent.jar"
  #echo $APP_ID
  # return 0 or ppid
  getPPID()
  {
  program=`ps -ef|grep $APP_ID|grep -v grep`
  if [ "$program" = "" ]; then
  echo "-1"
  else
  local ppid=$(echo "$program" | awk {'print $2'})
  echo "$ppid"
  fi
  }
  startConnectionAgent()
  {
  echo $APP_ID
  local ppid=$(getPPID)
  if [ "$ppid" != "-1" ]; then
  echo "WARN: $MAIN_CLASS is running!"
  return 1
  fi
  local jarFileLibs="."
  echo $JAVA_ARGS
  ${JAVA_BIN} -cp $jarFileLibs:/usr/local/OMCollectAgent/lib/*:$APP_ID $MAIN_CLASS $JAVA_ARGS
  sleep 1
  ppid2=$(getPPID)
  echo $ppid2
  if [ "$ppid2" != "-1" ]; then
  echo "INFO: Start $MAIN_CLASS successfully!"
  return 1
  else
  echo "ERROR: Start $MAIN_CLASS failed! Please check again."
  return -1
  fi
  }
  stopConnectionAgent()
  {
  local ppid=$(getPPID)
  if [ "$ppid" = "-1" ]; then
  echo "INFO: $MAIN_CLASS is not running!"
  return 0
  else
  kill -9 $ppid
  sleep 3
  ppid=$(getPPID)
  if [ "$ppid" = "-1" ]; then
  echo "INFO: Stop $MAIN_CLASS successfully."
  return 0
  else
  echo "WARN: Could not stop $MAIN_CLASS. Please check again."
  return -1
  fi
  fi
  }
  usage()
  {
  echo "usage:"
  echo "    $PRG start"
  echo "    $PRG stop"
  }
  #----------------------main----------------------#
  # resolve links - $0 may be a softlink
  PRG="$0"
  JAVA_ARGS="$2"
  echo $JAVA_ARGS
  while [ -h "$PRG" ]; do
  ls=$(ls -ld "$PRG")
  link=$(expr "$ls" : '.*-> \(.*\)$')
  if expr "$link" : '/.*' > /dev/null; then
  PRG="$link"
  else
  PRG=$(dirname "$PRG")/"$link"
  fi
  done
  echo $PRG
  # Get standard environment variables
  #PRGDIR=$(dirname "$2")
  #cd $PRGDIR
  #echo $PRGDIR
  #----------compile----------#
  #------------run------------#
  case "$1" in
  start)
  startConnectionAgent
  exit $?;;
  stop)
  stopConnectionAgent
  exit $?;;
  *)
  echo "$1 in usage"
  usage
  exit $?;;
  esac

页: [1]
查看完整版本: shell 脚本调用java进程