SoFunction
Updated on 2025-03-02

Springboot project uses nohup to specify the output file to log files and solutions

Springboot project uses nohup to specify the output file to be too large

Suppose there is currently a file with the following content:

#!/bin/sh
jarname='app-gmt'
pid=`ps aux | grep $jarname | grep -v grep | grep java | awk '{print $2}'`
if [ "$pid" == "" ]
  then
        echo $jarname"The process does not exist, Start directly"$jarname"system"
  else
        echo  "Ready to kill"$jarnamed"process: "$pid
        kill -9 $pid
fi
nohup java -jar $ --=online -server -Xms512m -Xmx1024m -Xss256k >> ./ 2>&1 &
echo "start success!!!"

Pay attention to the nohup … >> ./ 2>&1 & line. Here, the process's logs are output to the specified file. Many people should have done this.

Then, if you do this, you will have a situation that will get bigger and bigger until you explode.

How to solve it?

There are two steps:

1.Create a script to clean the log files first
	1.Direct clear method
	2.Keep some methods
2.Create another timed task(For example, execute the script at 5 a.m.)

The following is a detailed explanation of these two parts:

First point: This actually depends on the specific needs of individuals. For example, the conditions for clearing can be the size of the file to be processed. Some will retain part of it during processing, and some will be cleared out.

1. Let’s talk about the direct removal method first:

First create a script file, the content is as follows:

#!/bin/bash
# Set file pathFILE_PATH="/home/program/app/"
# Set the file size threshold, unit in bytesTHRESHOLD_SIZE=$((100 * 1024 * 1024))
# Get file sizeFILE_SIZE=$(du -b "$FILE_PATH" | cut -f1)
# Check if the file exceeds the thresholdif [ $FILE_SIZE -gt $THRESHOLD_SIZE ]; then
    # File exceeds the threshold, clear the file contents    echo -n > "$FILE_PATH"
    echo "File has been cleaned up."
else
    echo "File size is within threshold."
fi

If the log file reaches 100M, clear the file contents.
Remember to change FILE_PATH to your own log file path

Then add to the timing task:

crontab -e

The content is as follows:

0 5 * * * /data/log_clean/clean_app_log_file.sh

Remember to replace the path with your own file path

2. Keep some methods

Or create a script file:

log=`tail -n 10000 `; 
echo "$log" > /home/program/app/

Remember to change the path to your own log file path

Then create a timed task, the same as directly clearing it.

This is the article about the solution to using nohup to specify the output file of the log to specify too large output file in springboot project. For more related content of springboot nohup files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!