Write a general shell script to start a java project, which is applicable to most services. You only need to modify the service name

File name: service user sh

Document content:

##The header file of shell script must have #/ bin/sh

##Configure the Java environment variable again to avoid reporting other errors ## Java env#jdk installation directory export Java_ HOME=/usr/java/jdk1. 8.0_ 144export JRE_ HOME=$JAVA_ HOME/jre

## service nameAPP_ NAME=user

##Service folder_ DIR=/service/$APP_ Name ## service name service_ NAME=edu-service-$APP_ Name ##jar package name jar_ NAME=$SERVICE_ NAME\. Jar ## generate process file PID = $service_ NAME\. pid

##Enter the service storage folder CD $service_ DIR

##Receive a parameter input from the Linux console/ service-user. sh start/stop/restartcase "$1" in

##When the input parameter is start, start the project (start) ##nohup daemon starts the project, and the project is still running even after exiting the terminal/ Dev / null inputs the log to null, that is, it does not output the log to notout Out, because there are already log4j logs in the project. Nohup $JRE_ HOME/bin/java -Xms256m -Xmx512m -jar $JAR_ NAME >/dev/null 2>&1 & echo $! > $ SERVICE_ DIR/$PID echo "=== start $SERVICE_NAME" ;;

##Stop the project when the input parameter is stop) ## kill the process ID, i.e. edu service user pid kill `cat $SERVICE_ Dir / $PID ` # delete the process PID file RM - RF $service_ DIR/$PID echo "=== stop $SERVICE_NAME"

#Wait for 5S sleep 5 ##### edu service AA jar ## edu-service-aa-bb. Jar ## obtains the process ID - W, which is a full match. If - W is not added, it is a fuzzy match, which may cause the accidental killing of process P_ Id = ` PS - EF | grep - W "$service_name" | grep - V "grep" | awk '{print $2}' ` # if the process ID is empty, it will prompt that the process does not exist or has stopped if ["$p_id" = = ""]; Then echo "= = = $service_name process not exists or stop success" ## if the process ID is not empty, kill the process 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 the project when the input parameter is restart) $0 stop sleep 2 $0 start echo "= = = restart $service_name";;

##Restart the project when nothing is entered *) ## restart stop and restart first to avoid process conflict, because the process ID generated each time is different $0 stop sleep 2 $0 start;;

esacexit 0

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>