In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Linux general java program startup script how to write", the content is detailed, the steps are clear, the details are handled properly, I hope this "Linux general java program startup script how to write" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
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_ mainclass` 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 Read here, this "Linux general java program startup script how to write" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.