linux java 启动脚本

#!/bin/sh

## java env
#export JAVA_HOME=/data/jdk1.8.0_121
#export JRE_HOME=$JAVA_HOME/jre

## service name
#当前目录
SERVICE_DIR=$(cd `dirname $0`; pwd)
echo "======$SERVICE_DIR"
#jar包路径
JAR_DIR=`ls -ltr $SERVICE_DIR/*.jar| tail -1`
echo "JAR_DIR==== $JAR_DIR"
#jar包名字
SERVICE_NAME=${JAR_DIR##*/}
#日志名字
LOG_NAME=${SERVICE_NAME%.*}

PID=$LOG_NAME\.pid

if test ! -d "./logs/" ;then
    echo "logs/ directory does not exist,creating....."
    mkdir logs
fi

case "$1" in

start)
    nohup java -jar -Xms1024m -Xmx1024m $SERVICE_NAME  > logs/$LOG_NAME.log 2>&1 &
    echo $! > $SERVICE_DIR/$PID
    echo "=== start $SERVICE_NAME"
;;

stop)
    kill `cat $SERVICE_DIR/$PID`
    rm -rf $SERVICE_DIR/$PID
    echo "=== stop $SERVICE_NAME"

    sleep 5

    P_ID=`ps -ef | grep -w "$SERVICE_NAME" | grep -v "grep" | awk '{print $2}'`
    if test "$P_ID" == "" ; then
        echo "=== $SERVICE_NAME process not exists or stop success"
    else
        echo "=== $SERVICE_NAME process pid is:$P_ID"
        echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"
        kill -9 $P_ID
    fi
;;

restart)
    $0 stop
    sleep 2
    $0 start
    echo "=== restart $LOG_NAME"
;;

*)
    echo "Usage:$0 {start|stop|restart}"
;;

esac
exit 0

你可能感兴趣的:(linux,java,python)