Use Shell script to start/stop Java's jar program

This article describes how to use Shell scripts to start and stop the background running of the jar program, and how to split large out files. In addition, add some little knowledge about running in the background.

Startup script: start_upload.sh

#!/bin/sh
nohup java -jar UploadSchedule.jar >> /usr/local/tomcat9/webapps/upload-to-oss/nohup_upload.out 2>&1 &
echo $! > /var/run/UploadSchedule.pid

Startup script description:

1. Enable background execution, and write the log file generated by the program into the out file;
2. Write the current process id into /var/run/UploadSchedule.pid, so that this pid can be used in the stop script to stop the current process.

Stop script: stop_upload.sh

#!/bin/sh
PID=$(cat /var/run/UploadSchedule.pid)
kill -9 $PID

Stop script description:

1. First obtain the process id;
2. Then kill the process.

File segmentation: split_nohup.sh

If the program is not interrupted and keeps running in the background, the generated log will continue to be output to the out asking price, causing the out file to become larger and larger. It is very inconvenient when some log searches need to be performed based on the out file. Therefore, the out file is segmented regularly, and the out file of the current day is divided into several small files according to the date, so as to facilitate processing and error finding and positioning.

#!/bin/bash
log_path=/usr/local/tomcat9/webapps/upload-to-oss

if [ -f "$log_path/nohup_upload.out" ];then
current_date=`date -d "-1 day" "+%Y%m%d"`
cp $log_path/nohup_upload.out $log_path/nohup_upload_${current_date}.out
cat /dev/null > $log_path/nohup_upload.out
split -b 5242880 -d -a 4 $log_path/nohup_upload_${current_date}.out $log_path/nohup_upload_${current_date}_
rm -rf $log_path/nohup_upload_${current_date}.out
fi

Segmentation script description:

1. If the out file exists, copy the nohup_upload.out file and name it nohup_upload_current date (such as nohup_upload_20200328.out); 2.
Clear the nohup_upload.out file;
3. Split the nohup_upload_20200328.out file The divided file naming format is nohup_upload_20200328_0001, nohup_upload_20200328_0002;

  • -b specifies the size of each small file (unit byte)
  • -d suffix with a number
  • -a specifies the length of the suffix

4. Delete the original file (nohup_upload_20200328.out).

Execute start/stop command

# 启动
./start_upload.sh

# 停止
./stop_upload.sh

Some background knowledge

The difference between & and nohup:
& means to run in the background. It means that when you are executing ./start_upload.sh &, even if you use Ctrl + C, then start_upload.sh will still run (because it is immune to SIGINT signal). But pay attention, if you close the shell directly, then the start_upload.sh process will also disappear. Because it is not immune to SIGHUP signals.

nohup means to ignore the SIGHUP signal, so when running nohup ./start_upload.sh, close the Shell, the start_upload.sh process still exists (immune to the SIGHUP signal). But if you use Ctrl + C directly in the shell, then the start_upload.sh process will also disappear (because it is not immune to the SIGINT signal)

Therefore, to make the process really not affected by Ctrl + C in the Shell and the closing of the Shell, use nohup and & at the same time.

Guess you like

Origin blog.csdn.net/u011886447/article/details/105168021