Delete expired log files under Linux

Delete expired log files under Linux

Simply record the needs and problems encountered at work and write a blog to record your own growth.

There are requirements in the current project, and it is necessary to regularly process the expired log files on the server and the expired pictures stored locally on the server.

Ideally, the logs and pictures from 7 days ago are deleted. At present, this article records the deletion of logs and pictures on the local Linux server.

I think of 4 ways to achieve

1-Java timing tasks directly delete the local specified files, use filters to filter out 7-day files, and delete files directly. This method is the most straightforward, but the filtering logic and process are more complicated.

2-Linux writes and deletes logs and pictures 7 days ago, this only requires two lines of code, but the timed tasks need to be added to the Linux system, root privileges are required

3-Linux writes a delete script, and Java creates a timing task to execute this script. The expiration time and timing can be changed at will, which is more flexible, and the maintenance complexity is low.

4-Write the Linux script into a string format in Java, and use Java timing tasks to execute the script generated by this string. The most concise and convenient

I tried all 4 methods, and finally I chose the second one, which was quick and easy.
Of course, I also included the third method. I hope it will be helpful to you who read the article.

1. Linux scripting

Create a shell script touch + file absolute path

touch /mnt/data/opt/data/cow_spray_control_java/48b02d1854a9/shell/delete.sh

Assign executable permissions chmod +x + file absolute path

chmod +x /mnt/data/opt/data/cow_spray_control_java/48b02d1854a9/shell/delete.sh

Enter the directory and edit the script file

cd /mnt/data/opt/data/cow_spray_control_java/48b02d1854a9/shell

vim delete.sh

Linux automatically delete logs and instance commands n days ago

find /opt/log/ -mtime +7 -name "*.log" -exec rm -rf {} \;

find: Linux search command, the user searches for files with specified conditions;

/opt/log/: The directory where the logs to be cleaned up are stored;

-mtime +7: Find files 7 days ago, a number represents the number of days;

"*.Log": The type of file you want to find, " .jpg" means all the pictures of the tea table, inferiorly " *+ file suffix" means this kind of file

-exec: fixed writing;

rm -rf: delete files by force, novices should use it with caution, it is easy to delete all files and run away;

{};: A pair of curly brackets {} + space + backslash \ + semicolon;

Add crontab (if you do not use Java to execute, you can directly execute Linux timing tasks)
crontab -e
59 23 * * * /bin/sh /opt/log/logs_delete.sh

This means that the script is executed every day at 23:59.

2. Java timing task writing

shellPath is the absolute path of the shell script file

 @Scheduled(fixedDelay = 24 * 60 * 60 * 1000)
    public void run() {

        // 把要执行的脚本文件路径写成字符串格式传入exec执行脚本,注意linux下文件目录/使用反斜杠
        String[] scriptPath = new String[] {"/bin/sh", shellPath};
        try {
            LOGGER.info("定时清理任务开始");
            // 执行脚本
            Runtime.getRuntime().exec(scriptPath);
        } catch (IOException e) {
            LOGGER.error("定时清除脚本执行异常", e);
        }
        LOGGER.info("定时清理任务结束");
    }

Guess you like

Origin blog.csdn.net/weixin_43447239/article/details/113999104