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# the script is a generic script that launches Java programs under Linux. That is, it can be called as a boot service script, #也可以作为启动java程序的独立脚本来使用. # #Author: tudaxia.com, date:2011/6/7# #警告!!! : The script Stop section uses the System Kill command to force the termination of the specified Java program process. #在杀死进程前, no condition checks were made. In some cases, such as a program is in the process of file or database write operations, #可能会造成数据丢失或数据不完整. If you have to take this into account, you need to overwrite this script, #增加在执行kill命令前的一系列检查. ##################################### #环境变量及程序执行参数 # These parameters need to be modified according to the actual environment and the Java program name ################################### #JDK所在路径JAVA_HOME = "/usr/java/jdk" #执行程序启动所使用的系统用户, it is recommended not to use the root account running_user=root for security reasons # The directory where the Java program resides (the top-level directory of classes) App_home=/opt/tudaxia/test/web-inf #需要启动的Java主程序 (Main method Class) app_mainclass= Com.tudaxia.test.TestMain #拼凑完整的classpath参数, including all jarclasspath= $APP _home/classesfor i in "$APP _home"/lib/* specified in the Lib directory. Jar Do classpath= "$CLASSPATH": "$i" Done #java虚拟机启动参数JAVA_OPTS = "-MS512M-MX512M-XMN256M-DJAVA.AWT.HEADLESS=TRUE-XX: maxpermsize=128m "#################################### (function) to determine if the program has been started # #说明: #使用JDK自带的JPS命令及grep命令组合, exactly find PID#JPS plus l parameter That shows the full package path for Java # using awk, splitting out the PID (part), and the Java program name (###################################) #初始化psid变量 (Global) Psid=0 Checkpid () {javaps= ' $JAVA _home/bin/jps-l | grep $APP _mainclass ' If [-N ' $javaps]; then psid= ' echo $ Javaps | awk ' {print '} ' else psid=0 fi} #################################### (function) Launcher # #说明: #1. First call the Checkpid function and refresh the $PSID global variable # #. If the program has started ($psid not equal to 0), the prompt program has started # #. If the program is not started, execute the start command line # #. After starting the command execution, call the Checkpid function again # #. If the result of step 4 is able to confirm the PID of the program, print [OK], otherwise print [Failed] #注意: echo-n means no line break after printing characters # Note: "Nohup a command >/dev/null 2>&1 &" Usage # # # ############################## #start () {checkpid if [$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 Runni Ng "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 is not running ################################## #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/issue ' echo ' uname-a ' echo Echo ' java_home= $JAVA _home ' echo ' $JAVA _home/bin/java-version ' echo Echo ' a Pp_home= $APP _home "echo" app_mainclass= $APP _mainclass "echo" **************************** "} ####################### ############ #读取脚本的第一个参数 ($) 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.