Java应用启动、停止、重启Shell脚本模板

server.sh

#!/bin/bash #java options APP_MAINCLASS=com.woo.erp.WooERPMain PROC_NAME=WooERPMain CLASS_PATH=.:dependency/*:classes/ # 日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到 LOG_PATh=/data/application/woo-erp/nohup.out#Xms inin memery #Xmx max memery #Xmn young memery JAVA_OPTS="-Duser.timezone="GMT+8" -Xms512m -Xmx2G -XX:CompressedClassSpaceSize=128m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m -Xloggc:logs/gc.log -cp .:dependency/*:classes/ -Dspring.profiles.active=test" # 启动方法 start() { # 重新获取一下pid,因为其它操作如stop会导致pid的状态更新 pid=`ps -ef | grep $PROC_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行 if [ -z $pid ]; then nohup java $JAVA_OPTS com.woo.erp.WooERPMain /dev/null 2>&1 & #nohup java -jar $PROC_NAME $JAVA_OPTS /dev/null 2>&1 & pid=`ps -ef | grep $PROC_NAME | grep -v grep | awk '{print $2}'` echo "" echo "Service ${PROC_NAME} is starting!pid=${pid}" echo "........................Here is the log.............................." echo "....................................................................." # tail -f $LOG_PATh echo "........................Start successfully!........................." else echo "" echo "Service ${PROC_NAME} is already running,it's pid = ${pid}. If necessary, please use command: sh auto_deploy.sh restart." echo "" fi }# 停止方法 stop() { # 重新获取一下pid,因为其它操作如start会导致pid的状态更新 pid=`ps -ef | grep $PROC_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到 if [ -z $pid ]; then echo "" echo "Service ${PROC_NAME} is not running! It's not necessary to stop it!" echo "" else kill -9 $pid echo "" echo "Service stop successfully!pid:${pid} which has been killed forcibly!" echo "" fi }# 输出运行状态方法 status() { # 重新获取一下pid,因为其它操作如stop、restart、start等会导致pid的状态更新 pid=`ps -ef | grep $PROC_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到 if [ -z $pid ]; then echo "" echo "Service ${PROC_NAME} is not running!" echo "" else echo "" echo "Service ${PROC_NAME} is running. It's pid=${pid}" echo "" fi }# 重启方法 restart() { echo "" echo ".............................Restarting.............................." echo "....................................................................." stop start }info() { echo "System Information:" echo "****************************" echo `head -n 1 /etc/issue` echo `uname -a` echo "****************************" echo "JAVA_HOME=$JAVA_HOME" echo `$JAVA_HOME/bin/java -version` echo "****************************" echo "APP_MAINCLASS=$APP_MAINCLASS" echo "****************************" }if [ ! -n "$1" ] ; then echo "Usage: $0 {start|stop|restart|status|info}" elif [ $1 = "start" ]; then start elif [ $1 = "stop" ]; then stop elif [ $1 = "restart" ]; then restart elif [ $1 = "status" ]; then status elif [ $1 = "info" ]; then info fi

【Java应用启动、停止、重启Shell脚本模板】使用示例:
./server start #启动
./server stop #停止
./server restart #重启
./server status #查看启动状态
./server info #查看系统信息

    推荐阅读