Good Linux generic Java program startup script

Source: Internet
Author: User

Although the frequency of writing the shell is very low ... But every time you write a lot of jar file paths, the new jar package must also be modified to start the shell.

Find a nice generic shell script on the web.

Just modify some of the configuration variables to make the start script.

And in addition to being able to start, it also supports shutting down, restarting, and checking whether the feature is running.

Original address: HTTP://WWW.TUDAXIA.COM/ARCHIVES/10

In the start function, the nohup part can actually be put into a configuration variable. There is no modification here directly to the author's original text

#!/bin/sh#This script is a generic script that launches Java programs under Linux. That can be invoked as a boot service script,#It can also be used as a standalone script to start a Java program.##Author:tudaxia.com, DATE:2011/6/7##Warning!!! : The script Stop section uses the System Kill command to force the termination of the specified Java program process.#No condition checks were made before the process was killed. In some cases, such as a program is in the process of file or database write operations,#may result in data loss or incomplete data. If you have to take this into account, you need to overwrite this script,#Adds a series of checks before the KILL command is executed.######################################Environment variables and program execution parameters#These parameters need to be modified according to the actual environment and the Java program name####################################The path to your JDKJava_home="/usr/java/jdk" #The system user used to execute the program startup, in consideration of security, it is recommended not to use the root accountRunning_user=Root#The directory in which the Java program resides (the top-level directory of classes)App_home=/Opt/Tudaxia/Test/Web-Inf#Java main program to start (Main method Class)App_mainclass=Com.tudaxia.test.TestMain#Pieced together the complete classpath parameters, including specifying all the jars in the Lib directoryCLASSPATH=$APP _home/ClassesForIInch "$APP _home"/Lib/*. jar;DoCLASSPATH="$CLASSPATH":"$i"Done#Java Virtual machine startup parametersJava_opts="-ms512m-mx512m-xmn256m-djava.awt.headless=true-xx:maxpermsize=128m" ####################################(function) to determine whether a program has started##Description#Use the JDK's own JPS command and grep command combination to find the PID exactly#JPS plus L parameter, indicating the full package path for Java display#Use awk to split the PID (part), and the Java program name (the $ $ section)####################################Initialize the PSID variable (global)Psid=0Checkpid () {Javaps=`$JAVA _home/Bin/JPs-L|Grep$APP _mainclass`If[-N"$javaps"]; Then PSID=' Echo$javaps |awk ' {print$} 'ElsePsid=0Fi####################################(function) to start a program##Description#1. First call the Checkpid function to refresh the $PSID global variable#2. If the program has started ($psid not equal to 0), the prompt program has started#3. If the program is not started, execute the start command line#4. After starting the command execution, call the Checkpid function again#5. If the result of step 4 can confirm the PID of the program, print [OK], otherwise print [Failed]#Note: Echo-n indicates that no line breaks after printing characters#Note: The use of "nohup a command >/dev/null 2>&1 &"###################################Start () {CheckpidIf[$psid -ne 0]; Then Echo"================================"Echo"Warn: $APP _mainclass already started! (pid= $psid)"Echo"================================"Else Echo-n" starting $APP _mainclass ... " java_cmd= "Nohup $JAVA _home/bin/java $JAVA _opts-classpath $CLASSPATH $APP _mainclass >/dev/null 2>&1 &" Su -$RUNNING _user-c "$JAVA _cmd" checkpid if [$psid-ne 0]; Then echo "(pid= $psid) [OK]" Else echo "[Failed]" fi fi} #################################### (function) Stop program # #说明: #1. First call the Checkpid function and refresh the $PSID global variable # #. If the program has started ($psid not equal to 0), start execution Stop, otherwise the prompt program does not run # #. Use the kill-9 PID command to force kill process # #. Immediately after executing the KILL command line, look at the return value of the previous command: $? #5. If the result of step 4 is $ = 0, print [OK], otherwise print [Failed] #6. In order to prevent the Java program from being started multiple times, the process of repeated checking is added here (recursive call to stop). #注意: Echo-n means no line break after printing characters # Note: In Shell programming, "$?" means the previous command or the return value of a function ################################## #stop () {checkpid if [$ Psid-ne 0]; Then Echo-n "Stopping $APP _mainclass ... (pid= $psid) "Su-$RUNNING _user-c" kill-9 $psid "if [$?] -eq 0]; Then echo "[OK]" Else echo "[Failed]" fi checkpid if [$psid-ne 0]; Then stop fi else echo "================================" echo "warn: $APP _mainclass was not running" echo "=============== ============== = = "fi} #################################### (function) Check program run status # #说明: #1. First call the Checkpid function and refresh the $PSID global variable # #. If the program has started ($psid not equal to 0), the prompt is running and indicates pid#3. Otherwise, the prompt program does not run ################################## #status () {checkpid if [$psid-ne 0]; then echo "$APP _mainclass is running! (pid= $psid) "Else echo" $APP _mainclass is not running "fi} #################################### (function) printing system environment parameter ########### ####################### #info () {echo "System information:" echo "****************************" Echo ' Head-n 1/etc/issu E ' echo ' uname-a ' echo Echo ' java_home= $JAVA _home ' echo ' $JAVA _home/bin/java-version ' echo Echo ' app_home= $APP _home ' Ech O "app_mainclass= $APP _mainclass" echo "****************************"} #################################### Read the first parameter of the script ($) to determine the value range of the # parameter: {start|stop|restart|status|info} #如参数不在指定范围之内, print help information ############################### # # # #case "$" in ' start ') start; ' Stop ') stop; ' Restart ') stop start;; ' status ') status; ' info ') info;; *) echo "Usage: $ {Start|stop|restart|status|info}"Exit 1 

Good Linux generic Java program startup script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.