[Linux] Customize shell scripts to implement jar package management

The operations on the jar package in the linux system are generally divided into the following four types: start the jar package, stop the jar package, check the status of the jar package, and restart the jar package.

Existing jar package exam-admin.jar

Start the jar package:
first check the running status of the jar package, and manually start the jar package if it is not running.

java -jar exam-admin.jar &

Stop the jar package:
first you need to check the status of the jar package, if it is running, write down its process number, and close the jar package through the process number.

kill -9 pid //pid表示进程号

View the running status of the jar package:
You can view the running process through ps ax or ps -ef.

ps ax|grep exam-admin.jar

ps -ef | grep exam-admin.jar

Restart the jar package:
restarting the jar package is to close the jar package first (kill the process running the jar package), and then start the jar package (java - jar) .

It is very cumbersome to manage jar packages through the above methods, and the same code is often repeated, which is inefficient. Therefore, the jar package management can be realized by writing the code into the sh file by writing a shell script.

  1. First, create a sh script file in the directory where the jar package is located.
touch exam.sh

2. Enter edit mode

vim exam.sh //按i键开始编写文件

3. You can see that you need to check the running status of the process when you start, stop, etc., that is, check whether the jar package is running. Then edit a script menu item to realize four functions respectively, the code is as follows:

#!/bin/bash
#name:jar包启动脚本;
#date:20230126
#author:LiHaoran

APP_NAME=exam-admin.jar

usage() {
    
    
 echo "Usage: sh exam.sh [start|stop|restart|status]"    
 exit 1
}

is_exist(){
    
    
 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
 #如果不存在返回1,存在返回0,通过-z判断是否为空
 if [ -z "${pid}" ]; then
 return 1
 else
 return 0
 fi
}
#启动脚本
start(){
    
    
 is_exist
 if [ $? -eq "0" ]; then
 echo "${APP_NAME} is already running. pid=${pid} ."
 else
#此处注意修改jar和log文件文件位置:
 nohup java -jar /root/exam/target/$APP_NAME > bootdolog.file   2>&1 &
#此处打印log日志:
 tail -f /root/exam/target/bootdolog.file
 fi
}
#停止脚本
stop(){
    
    
 is_exist
 if [ $? -eq "0" ]; then
 kill -9 $pid
 else
 echo "${APP_NAME} is not running"
 fi
}
#显示当前jar运行状态
status(){
    
    
 is_exist
 if [ $? -eq "0" ]; then
 echo "${APP_NAME} is running. Pid is ${pid}"
 else
 echo "${APP_NAME} is NOT running."
 fi
}
#重启脚本
restart(){
    
    
 stop
 start
}

case "$1" in
 "start")
 start
 ;;
 "stop")
 stop
 ;;
 "status")
 status
 ;;
 "restart")
 restart
 ;;
 *)
 usage
 ;;
esac

4. After writing, press esc and enter: wq! Exit and save.
5. Test run:

sh exam.sh

sh exam.sh status //查看运行状态和进程号

insert image description here

Summarize

  • The difference between sh and ./ two execution script files

sh means that the script uses the sh script interpreter by default, use "sh" to execute the script, and the corresponding xxx.sh can be executed without execution permission.
If the script interpreter is not specified, the default is ./, use "./" to execute the script, and the corresponding xxx.sh script must have execution permission.

  • nohub command

Run the command without hanging up, and continue to run the corresponding process after logging out of the account.
java -jar a.jar &
directly starts the jar file, starts a sub-process to run the program in the current session process, and this sub-process will end with the end of the session process.

nohup java -jar a.jar&
If you are running a process and you feel that the process will not end when you log out of the account, you can use the nohup command. This command can continue to run the corresponding process after you log out of the account/close the terminal.

Guess you like

Origin blog.csdn.net/qq_44878985/article/details/128766140