#!/bin/bash # 配置参数 JAVA_HOME="/usr/local/java/jdk"; JAR_NAME="appservice.jar"; JAR_PORT=8083 JAVA_OPTS=" -server -Xmx1024m -Xms1024m -Duser.timezone=GMT+08 -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxTenuringThreshold=15 -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump/ -XX:ErrorFile=hss_err_pid.log" # 检查应用进程号,保存在psid变量 psid=0 checkpid() { javaps=`$JAVA_HOME/bin/jps -l | grep "\<${JAR_NAME}\>"` if [ -n "$javaps" ]; then psid=`echo $javaps | awk '{print $1}'` else psid=0 fi } # 启动应用 start() { checkpid if [ $psid -ne 0 ]; then echo "================================" echo "warn: $JAR_NAME already started! (pid=$psid)" echo "================================" else echo -n "Starting $JAR_NAME ..." nohup $JAVA_HOME/bin/java -jar $JAVA_OPTS $JAR_NAME --server.port=$JAR_PORT >nohup.out 2>&1 & checkpid if [ $psid -ne 0 ]; then echo "(pid=$psid) [OK]" else echo "[Failed]" fi fi } # 停止应用 stop() { checkpid if [ $psid -ne 0 ]; then echo -n "Stopping $JAR_NAME ...(pid=$psid) " kill -15 $psid if [ $? -eq 0 ]; then echo "[OK]" else echo "[Failed]" fi sleep 1 checkpid if [ $psid -ne 0 ]; then stop fi else echo "================================" echo "warn: $JAR_NAME is not running" echo "================================" fi } case $1 in start) echo "start project......" start ;; stop) echo "stop project......" stop ;; restart) echo "restart project......" stop start ;; *) esac exit 0