#!/bin/sh #该脚本为Linux下启动java程序的通用脚本. 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命令前的一系列检查. # # ################################### #环境变量及程序执行参数 #需要根据实际环境以及Java程序名称来修改这些参数 ################################### #JDK所在路径 #JAVA_HOME =/root/jdk/jdk1.6.0_24 Echo $JAVA _home #执行程序启动所使用的系统用户, 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=/usr/local/nmagent #需要启动的Java主程序 (Main method Class) app_mainclass= Com.eastelsoft.netmanager.agent.Application #拼凑完整的classpath参数, including all jar classpath= in the specified Lib directory $APP _home for i in "$APP _ HOME "/LIB/*.*; 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 started # #说明: #使用JDK自带的JPS命令及grep命令组合, find the PID #jps plus l parameter accurately That shows the full package path for Java #使用awk, splits the PID (part), and the Java program name ($Part 2) ################################### #初始化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 to refresh the $PSID global variable #2. If the program has been started ($psid not equal to 0), the prompt program has started #3. If the program is not started, execute the start command line #4. Once you start the command execution, call the Checkpid function again #5. If the result of step 4 is able to confirm the PID of the program, print [OK], otherwise print [Failed] #注意: Echo-n means that after printing a character, the line is not wrapped #注意: "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 to refresh the $PSID global variable #2. If the program has started ($psid not equal to 0), start execution Stop, otherwise the prompt program is not running #3. Use the kill-9 PID command to force the kill process #4. 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 indicates that after printing a character, it does not wrap #注意: in shell programming, "$?" represents 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 to refresh the $PSID global variable #2. If the program has started ($psid not equal to 0), the prompt is running and indicates the PID #3. Otherwise, the prompt program does not run ################################### status () {checkpid if [$psid-ne 0]; then echo "$APP _mainclass is Runnin G! (pid=$PSID) "Else echo" $APP _mainclass is not running "fi} ################################### # (function) printing system environment Parameters ################ ################### info () {echo "System information:" echo "****************************" Echo ' head-n 1/etc/issue ' EC Ho ' uname-a ' echo Echo ' java_home= $JAVA _home ' echo ' $JAVA _home/bin/java-version ' echo Echo ' app_home= $APP _home ' echo ' AP p_mainclass= $APP _mainclass "echo" **************************** "} ################################### #读取脚本的第一个参数 ($ 1), to determine #参数取值范围: {start|stop|restart|status|info} #如参数不在指定范围之内, print help ################################### case "$" In ' start ') start; ' Stop ') stop; ' Restart ') stop start;; ' status ') status; ' info ') info;; *) echo "Usage: $ {Start|stop|restart|status|info}" Exit 1 Esac Exit 0
Scripts to start the Java service under Linux