Although the frequency of starting the shell is very low. . . But every time I write, I have to deal with a lot of jar file paths, and I have to modify the startup shell when adding new jar packages. Found a good general shell script on the Internet. Just modify some configuration variables and you can use it as a startup script. In addition to being able to start, it also supports shutting down, restarting, and checking whether it is running. In the start function, the nohup part can actually be taken out and put into a configuration variable. The original text of the author is pasted here without modification The script code is as follows: #!/bin/sh #This script is a general script for starting a Java program under Linux. That is, it can be called as a self-starting 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 stop part of this script uses the system kill command to forcibly terminate the specified Java program process. #No conditional checks are done before killing the process. In some cases, such as when a 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 the script. #Add 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 Java program name#################################### #JDK path JAVA_HOME="/usr/java/jdk" #The system user used to start the execution program. For security reasons, it is recommended not to use the root account RUNNING_USER=root #The directory where the Java program is located (the upper level directory of classes) APP_HOME=/opt/tudaxia/test/WEB-INF #The Java main program that needs to be started (main method class) APP_MAINCLASS=com.tudaxia.test.TestMain #Put together the complete classpath parameter, including all jars in the specified lib directory CLASSPATH=$APP_HOME/classes for i in "$APP_HOME"/lib/*.jar; do CLASSPATH="$CLASSPATH":"$i" done #java virtual machine startup parameters JAVA_OPTS="-ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m" ################################### #(Function) Determine whether the program has been started# #illustrate: #Use the JPS command and grep command combination that comes with JDK to accurately find the pid #jps plus l parameter means displaying the full package path of java #Use awk to split out the pid ($1 part) and the Java program name ($2 part) ################################### # Initialize 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# #illustrate: #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), it will prompt that the program has been started#3. If the program has not been started, execute the startup command line#4. After the startup command is executed, 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 means no line break after printing characters#Note: Usage of "nohup command>/dev/null 2>&1 &"#################################### 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 the program# #illustrate: #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), then start to stop it, otherwise, it will prompt that the program is not running#3. Use the kill -9 pid command to force kill the process#4. Execute the kill command line immediately and check the return value of the previous command: $? #5. If the result of step 4, $?, is equal to 0, then print [OK], otherwise print [Failed] #6. In order to prevent the Java program from being started multiple times, we add the process of repeatedly checking and killing the process (recursively calling stop). #Note: echo -n means no line break after printing characters#Note: In shell programming, "$?" means the return value of the previous command or 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 is not running" echo "=================================" fi } ################################### #(Function) Check program running status# #illustrate: #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), it will prompt that it is running and indicate the pid #3. Otherwise, it will 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 parameters##################################### 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 ($1) of the script and make a judgment#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 1 esac; The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Vue3+TypeScript implements a complete example of a recursive menu component
>>: Detailed explanation of MySQL string concatenation function GROUP_CONCAT
background The popularity of Docker is closely re...
1. Stop the mysqld.exe process first 2. Open cmd ...
1. Use Centos image to build local yum source Sin...
Implementation principle The main graphics are co...
question The tomcat container was successfully ad...
MySQL version: MySQL Community Edition (GPL) ----...
Get ip tool import lombok.extern.slf4j.Slf4j; imp...
Today I will introduce the most basic functions of...
Achieve results html <h2>CSS3 Timeline</...
Preface I'm currently working on the data ana...
Table of contents Boot Options Command Line Long ...
MySQL is a very powerful relational database. How...
The following is a bar chart using Flex layout: H...
Pure js implements a single-click editable table ...
question When I was writing a project function to...