Linux下jar包的启动,停止,重启脚本

脚本一:不带配置文件


#!/bin/bash
app_home="/opt/jar"
now=$(date +%Y%m%d)
command='java -Xloggc:./history.log -XX:+UseCompressedOops -XX:+HeapDumpOnOutOfMemoryError -Xms3G -Xmx3G -jar ./opc-temp-history-sink-0.0.1-SNAPSHOT.jar server'


start(){
    cd $app_home
	if [ ! -x "./console" ]; then
	  mkdir ./console
	fi
	log_file_url="./console/my_console_${now}.log"
	touch $log_file_url
	chmod -R 777 ./console
	
    if [ "$log_file_url" != "" ]; then
        exec $command  > "$log_file_url" &
    else
        exec $command   &
    fi
}

stop(){  
 ps -ef | grep "$command" | awk '{print $2}' | while read pid  
 do 
    C_PID=$(ps --no-heading $pid | wc -l)
    echo "current PID=$pid"
    if [ "$C_PID" == "1" ]; then
        echo "PID=$pid ready to kill my service "
        kill -9 $pid
        echo "PID=$pid my service has bean killed"
    else
        echo "PID=$pid my service not exist"
    fi 
 done  
}


case "$1" in  
start)  
start  
;;  
stop)  
stop  
;;    
restart)  
stop  
start  
;;  
*)  
printf 'Usage: %s {start|stop|restart}\n' "$prog"  
exit 1  
;;  
esac

脚本二、带配置文件


#!/bin/bash
app_home="/opt/tw-kayou"
now=$(date +%Y%m%d)
command='java -Xloggc:./gc.log -XX:+UseCompressedOops -XX:+HeapDumpOnOutOfMemoryError -Xms2G -Xmx2G -jar ./tw-kayou-service-0.0.1-SNAPSHOT.jar server ./application.yml'


start(){
    cd $app_home
	if [ ! -x "./console" ]; then
	  mkdir ./console
	fi
	log_file_url="./console/my_console_${now}.log"
	touch $log_file_url
	chmod -R 777 ./console
	
    if [ "$log_file_url" != "" ]; then
        exec $command  > "$log_file_url" &
    else
        exec $command   &
    fi
}

stop(){  
 ps -ef | grep "$command" | awk '{print $2}' | while read pid  
 do 
    C_PID=$(ps --no-heading $pid | wc -l)
    echo "current PID=$pid"
    if [ "$C_PID" == "1" ]; then
        echo "PID=$pid ready to kill my service "
        kill -9 $pid
        echo "PID=$pid my service has bean killed"
    else
        echo "PID=$pid my service not exist"
    fi 
 done  
}


case "$1" in  
start)  
start  
;;  
stop)  
stop  
;;    
restart)  
stop  
start  
;;  
*)  
printf 'Usage: %s {start|stop|restart}\n' "$prog"  
exit 1  
;;  
esac

 脚本三

#!/bin/bash
app_home="/opt/jar"
now=$(date +%Y%m%d)
command='java -Xloggc:./gc/monitor.log -XX:+UseCompressedOops -XX:+HeapDumpOnOutOfMemoryError -XX:MaxNewSize=5G -Xms5G -Xmx5G -jar ./opc-monitor-0.0.1-SNAPSHOT.jar server'


start(){
    cd $app_home
	if [ ! -x "./console" ]; then
	  mkdir ./console
	fi
	log_file_url="./console/my_console_${now}.log"
	touch $log_file_url
	chmod -R 777 ./console
	
    if [ "$log_file_url" != "" ]; then
        exec $command  > "$log_file_url" &
    else
        exec $command   &
    fi
}

stop(){  
 ps -ef | grep "$command" | awk '{print $2}' | while read pid  
 do 
    C_PID=$(ps --no-heading $pid | wc -l)
    echo "current PID=$pid"
    if [ "$C_PID" == "1" ]; then
        echo "PID=$pid ready to kill my service "
        kill -9 $pid
        echo "PID=$pid my service has bean killed"
    else
        echo "PID=$pid my service not exist"
    fi 
 done  
}


case "$1" in  
start)  
start  
;;  
stop)  
stop  
;;    
restart)  
stop  
start  
;;  
*)  
printf 'Usage: %s {start|stop|restart}\n' "$prog"  
exit 1  
;;  
esac

 

执行命令:

./start.sh start/stop/restart

Guess you like

Origin blog.csdn.net/wd520521/article/details/119449851