Log timing cut, and timed deletion (2 methods)

Source: Internet
Author: User

There are many ways to cut Catalian logs, such as shell scripts, cronolog or configuration log4j, and so on. But today I'm going to introduce the use of logrotate to generate logs by date.

If the size of the catalina.out exceeds 2G, Tomcat may crash without error prompting and cannot start. So in order to avoid this problem, we should divide catalina.out every day.

How to cut the catalina.out automatically or when the size reaches 5M per day catalina.out

1. The first method

#!/bin/sh# need to split the log path, the script is generally only modify this path can be used Log_path=/home/webapp/tomcat/logs# get current date D='Date +%y-%m-%d'#获取30天前的日期d90='date-d' -Day ago'+%y-%m-%d'#cd到log_path路径下 and copy the log file to the Cron directory CD ${log_path}&& CP Catalina. outCatalina. out. $d. log# empty the original log file echo> Catalina. out#移除30天前的日志文件rm-RF $log _path/cron/catalina. out. ${d90}.log

- * * * SH script directory * /* *.sh  : Wq Save and exit. Crontab timed task configuration complete. You can check whether the setting is successful by crontab-l

The second type:

1. Create a new file

Create a new file in the/etc/logrotate.d/directory, and name it arbitrarily.

1/etc/logrotate.d/tomcat
2. Copy the following statement to the new file above
1/var/log/tomcat/catalina.out {   copytruncate   daily   rotate 7   compress   Missingok   size 5M  }

Specific meanings of each configuration:

    • Make sure /var/log/tomcat/catalina.out points to the address of the catalina.out in your server
    • Daily -cut daily
    • rotate -up to seven files reserved
    • Compress -compress the split file
    • size -split when Catalina.out is greater than 5M
How it works
    1. Every night, cron daemon automatically performs tasks under the /etc/cron.daily directory.
    2. This will trigger the /etc/cron.daily/logrotate file, Logrotate is usually installed with Linux. It will execute this command-"/etc/sbin/logrotate/etc/logrotate.conf" (note space)
    3. /etc/logrotate.conf contains all the scripts in the /etc/logrotate.d directory
    4. This will trigger the previous step you wrote the /etc/logrotate.d/tomcate file
Perform logrotate manually

Execute the following statement to run the cron task manually

1/usr/sbin/logrotate/etc/logrotate.conf
More operations for Logrotate
1 Mans logrotate

Log timing cut, and timed deletion (2 methods)

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.