Linux general java program startup script code example

Linux general java program startup script code example

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:
  • How to use Java program (JSch) to run shell scripts on remote Linux hosts
  • Execute Java background code method analysis in js script
  • Java Execute JS Script Tool
  • Java program to call and execute shell scripts and problem summary (recommended)
  • When executing a Java request, the JVM cannot exit when the script execution ends
  • Detailed explanation of Jmeter calling java script process
  • How to run java program jar with shell script
  • How to prevent JS script injection in Java

<<:  Vue3+TypeScript implements a complete example of a recursive menu component

>>:  Detailed explanation of MySQL string concatenation function GROUP_CONCAT

Recommend

CSS sets Overflow to hide the scroll bar while allowing scrolling

CSS sets Overflow to hide the scroll bar while al...

Professional and non-professional web design

First of all, the formation of web page style main...

How to Communicate with Other Users on the Linux Command Line

It's easy to send messages to other users in ...

How to customize at and cron scheduled tasks in Linux

There are two types of scheduled tasks in Linux s...

How to quickly log in to MySQL database without password under Shell

background When we want to log in to the MySQL da...

11 common CSS tips and experience collection

1. How do I remove the blank space of a few pixels...

Why is the MySQL auto-increment primary key not continuous?

Table of contents 1. Introduction 2. Self-increme...

Detailed explanation of the group by statement in MySQL database group query

1: Statement order of grouping function 1 SELECT ...

Usage of Vue filters and timestamp conversion issues

Table of contents 1. Quickly recognize the concep...

How to change the root password in a container using Docker

1. Use the following command to set the ssh passw...

HTML basics - CSS style sheets, style attributes, format and layout details

1. position : fixed Locked position (relative to ...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

CentOS 7 builds hadoop 2.10 high availability (HA)

This article introduces how to build a high-avail...