SoFunction
Updated on 2025-03-04

Linux starts Java program jar package shell script method

Linux starts Java program jar package shell script

When deploying Java projects such as SpringBoot in Linux environment, you always need to repeatedly enter some commands

Here, the command is written into an executable shell script. It only needs to be placed in a directory of the same level as the jar package, and then modify some built-in variables in the script before it can be used normally.

Script usage

  • Create a script named (the script name is as per your own idea) and store it in the same level directory as the jar package you need to start
  • The script content is consistent with the one provided in this article. Remember to modify the value of the APP_NAME parameter.
  • Enter the command bash status in the storage path to view the running status of the current jar package
  • Enter the command sh start to run the project
  • There are four types of commands supported, namely: start|stop|restart|status

For specific functions, you can view the script code

#!/bin/bash

#Replace it with the program location here #Note: APP_NAME is the jar file name (not the path), and there cannot be spaces after "="#The APP_NAME here is the packaged jar package name#Note: If the package name carries the date or other content that will change, such as xxxx-1.0.0., please modify the name here to a fixed and identifiable name of the service, and replace the start command in the start method.APP_NAME=
#Add jar package path, so that scripts can be run in any directory to start the serviceAPP_PATH=/root/lcz

#Instructions for use, to prompt for input parametersusage() {
    echo "Please enter the action you performed: [start|stop|restart|status]"
    echo "For example: bash/start"
    exit 1
}

#Check whether the program is runningis_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
  #If it does not exist, it returns 1, it returns 0  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}

#Output result after starting the commandstart_log(){
  is_exist
  if [ $? -eq 0 ]; then
    echo "${APP_NAME} Started successfully!  pid=${pid}"
     tail ${APP_PATH}/ -f
  else
    echo "${APP_NAME} Startup failed!Please check and try again"
  fi
}

#Start methodstart(){
  is_exist
  if [ $? -eq 0 ]; then
    echo "${APP_NAME} is already running. pid=${pid}"
  else
    nohup java -jar -Xmx512m -Xms512m ${APP_PATH}/${APP_NAME} > ${APP_PATH}/ &
    #If APP_NAME carries variable content, please replace the start command below. The symbol * indicates the changed part of the jar package name    #nohup java -jar -Xmx512m -Xms512m ${APP_PATH}/${APP_NAME}*.jar >${APP_PATH}/ >/dev/null 2>&1 &
    start_log
  fi
}

#Stop Methodstop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "${APP_NAME} Closed! pid=${pid}"
  else
    echo "${APP_NAME} is not running"
  fi

  #Speciality of this script: Make sure to close all openoffice processes  soffice_stop
}

#Output running statusstatus(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is not running."
  fi
}

#Restartrestart(){
  stop
  echo "${APP_NAME} Prepare to restart..."
  sleep 5
  start
}

#Close openofficesoffice_stop(){
  soffice_pid=`ps -e|grep  |awk '{print $1}'`
  if [ -n "${soffice_pid}" ]; then
    kill -9 $soffice_pid
    echo "Closed"
  fi
}


#Select the corresponding method according to the input parameters, and if you do not enter, execute the instructions for usecase "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.