Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux general java program startup script

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the Linux general java program startup script, the content is clear, interested friends can learn, I believe it will be helpful after reading.

Although the frequency of writing startup shell is very low. But each time you have to write, you have to deal with a lot of jar file paths, and adding a new jar package also has to modify the startup shell.

Find a good general shell script on the Internet.

You only need to modify some configuration variables and you can use it as a startup script.

In addition to being able to start, it also supports the functions of shutting down, restarting, and checking to see if it is running.

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

The script code is as follows:

#! / bin/sh# this script is a general script for starting java programs under Linux. It can be called as a boot service script, and # can also be used as a stand-alone script to start a java program. # # Author: tudaxia.com, Date: 2011 java 6and7 warning!!: the stop part of the script uses the system kill command to force the termination of the specified java program process. # No conditional check was made before killing the process. In some cases, if the program is writing to a file or database, # may cause data loss or incomplete data. If you have to take this into account, you need to rewrite this script, # adding a series of checks before executing the kill command. # # Environment variables and program execution parameters # these parameters need to be modified according to the actual environment and the name of the Java program # # the path where JDK is located: JAVA_HOME= "/ usr/ Java/jdk "# the system user used to start the executor For security reasons, it is recommended not to use the directory where the root account RUNNING_USER=root # Java program is located (one level above the classes) APP_HOME=/opt/tudaxia/test/WEB-INF # needs to start the Java main program (main method class) APP_MAINCLASS=com.tudaxia.test.TestMain # piece together the complete classpath parameters, including all jarCLASSPATH=$APP_HOME/classesfor I in "$APP_HOME" / lib/*.jar under the specified lib directory Do CLASSPATH= "$CLASSPATH": "$I" done # java virtual machine startup parameter JAVA_OPTS= "- ms512m-mx512m-Xmn256m-Djava.awt.headless=true-XX:MaxPermSize=128m" # (function) to determine whether the program has been started # # description: # use the combination of JPS and grep commands included in JDK to accurately find the pid#jps plus l parameters Indicates the full package path to display java # using awk, split pid ($1 part), and Java program name ($2 part) # # initialize the psid variable (global) psid=0 checkpid () {javaps= `$JAVA_HOME/bin/jps-l | grep $APP_ MAINCLAS` if [- n "$javaps"] Then psid= `echo $javaps | awk'{print $1} '`else psid=0 fi} # # (function) start the program # # description: # 1. First call the checkpid function to refresh the $psid global variable # 2. If the program has been started ($psid is not equal to 0), prompt that the program has started # 3. If the program is not started, execute the startup command line # 4. After the startup command is executed, call the checkpid function # 5. If the result of step 4 can confirm the pid of the program, print [OK], otherwise print [Failed] # Note: echo-n means no new lines after printing characters # Note: "nohup 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 # # description: # 1. First call the checkpid function to refresh the $psid global variable # 2. If the program has been started ($psid is not equal to 0), the execution is stopped, otherwise, the program is not running # 3. Use the kill-9 pid command to forcibly kill process # 4. Immediately after executing the kill command line, check the return value of the previous command: $? # 5. If the result of step 4 is $? Equal to 0, print [OK], otherwise print [Failed] # 6. In order to prevent the java program from being started multiple times, the process of checking and killing repeatedly (recursively calling stop) is added here. # Note: echo-n means no new lines after printing characters. # Note: in shell programming, "$?" Represents the return value of the previous command or 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 is not running" echo "= =" fi} # # (function) check the running status of the program # # description: # 1. First call the checkpid function to refresh the $psid global variable # 2. If the program has been started ($psid is not equal to 0), the prompt is running and indicates pid#3. Exe. Otherwise, prompt that the 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) print 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" APP_HOME=$APP_HOME "echo" APP_MAINCLASS=$APP_MAINCLASS "echo" * "} # # # read the first parameter of the script ($1) Judge # Parameter value range: {start | stop | restart | status | info} # if the parameter is not within the specified range, print help information # # case "$1" in 'start') start ; 'stop') stop;;' restart') stop start;; 'status') status;;' info') info;; *) echo "Usage: $0 {start | stop | restart | status | info}" exit 1esac; after reading the above, do you have a better understanding of Linux's general java program startup script? if you want to learn more, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report