Linux uses timed tasks to clean up log _linux 45 days a week

Source: Internet
Author: User

This article is mainly about the use of the regular task of Linux to clean up 45 days before the weekly log. The server generates a large log file on a daily basis, so that the log files need to be cleaned up periodically in order to keep the hard disk from being stuffed with log files. At this point we can write a shell script to clean up the log 45 days before a certain path, and then set a scheduled task to execute the script regularly every week.

① to clean up log scripts under a path deloldlogs.sh:

[root@prx01 cleanlog]# vim/usr/local/cleanlog/deloldlogs.sh
#!/bin/sh
#删除输入路径下的修改时间在45天以前的日志文件 Find
$ 1-mtime +45-name "*log*"-exec rm-f {} \;

This code is not difficult to understand, is to clean up the path of parameter 1 under the modified time of 45 days before the log file

② the script that specifies which paths to clean up the log del_all_oldlogs.sh:

[root@prx01 cleanlog]# vim/usr/local/cleanlog/del_all_oldlogs.sh
#!/bin/bash 
/usr/local/cleanlog/ Deloldlogs.sh "/home/usr/ewp/logs"
/usr/local/cleanlog/deloldlogs.sh "/home/usr/h5/logs"
/usr/local/ Cleanlog/deloldlogs.sh "/home/usr/payment/logs"

③ Add execute permissions to the script:

[root@prx01 cleanlog]# chmod a+x/usr/local/cleanlog/del*.sh

④ Add timed Task:

[ROOT@PRX01 cron]# Vim/var/spool/cron/root

Add the following:

0 * * 6/usr/local/cleanlog/del_all_oldlogs.sh

Note:

    1. To prevent a lack of permissions, a root user's timed task is set here
    2. This code means: Execute/usr/local/cleanlog/del_all_oldlogs.sh this script at 6 0:10 a week

⑤ View timed tasks:

[Root@app05 logs]# Crontab-l

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.