Shell script timed task--log compression (delete)

Source: Internet
Author: User
Tags log log

A problem encountered earlier is that the log file is too large to occupy the disk space and cause the build project to fail

So I wanted to write a timed task. Delete a log or compress a log file

I've been on the internet for a long time and haven't found the right script.

So I consulted others, wrote a, share to the not found and need such a script novice students

Advantages: Timed compression, save space

Find log files that need to be compressed or deleted on demand

Named rm_log.sh

The shell script is as follows:

#!/bin/bash

Date

CD ${home}

Tomcat_loglist= ' Find. -type d-name "Logs"

Fortomcat_dir in $TOMCAT _loglist;

Todo

CD ${home} && CD ${tomcat_dir}&& CD.

If [-D "bin"] &&[-D "WebApps"]&& [-D "Conf"] && [D "Lib"]; Then

CD ${home} &&cd${tomcat_dir}

file_list=$ (find.-name "catalina.*"-mtime +2|grep-v ". Gz")

For i in ${file_list};

Todo

echo "Iis${tomcat_dir} ${i}"

Gzip ${i}

Done

Fi

Done

Of course you need to give him a permission to execute, or it will show a red error.

The next step is to add the timing.

To open a scheduled task edit using the command crontab-e

There are no scheduled tasks before the first line to start editing, and some words are edited in the following order,

It is recommended to comment on a line on a scheduled task


# Log Compression task

0 5 * * */**/**/**/rm_log.sh>>/**/**/**/rm_log.log


Save exit.

ps:0 5 * * * time, daily five o ' clock execution

/**/**/**/rm_log.sh is the path to your script

>>/**/**/**/rm_log.log Log Redirection

Too good to finally solve a problem ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.