# Graceful shutdown function gracefulShutdown(){ if [ -z "$PIDS" ]; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE does not started!" | tee -a $STDOUT_FILE else echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE kill $PIDS begining" | tee -a $STDOUT_FILE for PID in $PIDS ; do kill $PID > /dev/null 2>&1 done
# check for graceful shutdown COUNT=0 while [ $COUNT -lt 1 ]; do sleep 1 COUNT=1 for PID in $PIDS ; do PID_EXIST=`ps -f -p $PID | grep java` if [ -n "$PID_EXIST" ]; then COUNT=0 break fi done done echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE kill $PID success" | tee -a $STDOUT_FILE fi }
function operate(){ if [[ "$1" = "kill" ]]; then gracefulShutdown elif [[ "$1" = "start" ]] ; then # check JAVA_HOME ENV if [[ -z ${JAVA_HOME} ]]; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE 未配置 JAVA_HOME 环境变量" | tee -a $STDOUT_FILE exit 1 fi echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE JAVA_HOME=${JAVA_HOME}" | tee -a $STDOUT_FILE cd $PROJECT_HOME # starting nohup $JAVA_HOME/bin/java $JAVA_OPT >> $STDOUT_FILE 2>&1 & PIDS=`ps -ef | grep java | grep -v grep | grep "$JAR_FILE" | awk '{print $2}'` echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE started OK! pid: $PIDS" | tee -a $STDOUT_FILE else echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE is not support $1" | tee -a $STDOUT_FILE fi }
if [[ "$1" = "start" || "$1" = "check" ]]; then if [ -n "$PIDS" ]; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE already started! pid: $PIDS" >> $STDOUT_FILE exit 1 fi operate start elif [[ "$1" = "" || "$1" = "restart" ]]; then operate kill echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE starting" | tee -a $STDOUT_FILE ls -d -t $LOGS_DIR/undertow* | tail -n +3 | xargs rm -rf operate start elif [[ "$1" = "kill" ]]; then operate kill else echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE is not support $1" | tee -a $STDOUT_FILE fi