"Beginners Learn Linux" Cron job periodically delete log (log) files

Source: Internet
Author: User

You've been doing Windows development before, and Linux is required in recent projects. As a small rookie, hurriedly bought a classic book "Bird Brother's Linux private dishes" study. Recently there was a small task- because the product generated a lot of log, and grew fast, so you need to use the script (Bash scripts) to delete the expired log file .

Using cron job under Linux can be a good solution to this problem.

  What is a cron Job?

  To create a cron job, you need to use the command crontab, Wikipedia defines: crontab commands are commonly used in Unix-and Unix-like operating systems to set the periodic execution of instructions .

Consult some of the information (found technical query or to use Google) reference, during the period also encountered a lot of problems, through groping and learning, the implementation steps are as follows:

Write a bash shell script, function: Retrieve all log files under the Logs folder, query the date of each file, if the date expires, delete the log file  

1#!/bin/Bash2 3Log_path=""#此处定义你的日志文件夹路径4Expried_time=7#此处定义你的日志过期时间, such as 7 days5 6 functiondeletelogs () {7 # Get system time, all time formats are seconds8Local currentdate= 'Date+%s '9     Echo "Current Date:"$currentDateTen  One      for file inch`Find$1-name"*.js_*.log"' #此处定义文件名格式 to avoid accidental deletion  A      Do -Local name=$file -Local modifydate=$ (Stat-C%Y $file) the  - #对比时间, calculate the time of the log, the last modification -Local logexisttime=$ ($currentDate-$modifyDate)) -logexisttime=$ (($logExistTime/86400)) +          -         if[$logExistTime-gt $expried _time]; Then +             Echo "File:"$name"Modify Date:"$modifyDate +"Exist Time:"$logExistTime +"Delete:yes" A             RM-F $file at         Else -             Echo "File:"$name"Modify Date:"$modifyDate +"Exist Time:"$logExistTime +"Delete:no" -         fi -      Done - } -  inDeletelogs $log _path

Two. Create a cron Job that periodically executes the above script


1 sudo crontab–e 2 5 0 * * */home/user/deleteoldlog.sh >>/home/user/cron_job.log 2>&1


1. Here I use sudo crontab-e, not crontab-e, this is done using root crontab, you can use sudo to get root certain permissions.

2. The Cron job format is as follows:

1 * * * * * command to be executed2 -----3 | | | | | 4 | | | | -----Day of Week (0-7) (sunday=0 or 7)5 | | | -------Month (1-12)6 | | ---------Day of month (1-31)7 | -----------Hour (0-23)8 -------------Minute (0-59)

Refer to the usage of the Http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/,Cron job for details.


3. How do I check the execution of cron jobs through the logs?

The function of ">>/home/user/cron_job.log 2>&1" is to conveniently log logs of cron job execution to the log file of its own, so as to make it easier to view job execution status. In addition, the following command can be used to view the job execution of some other information, the main feeling is to look at their own specified log files, if the implementation of errors, such as Permisson denied error, recorded in the very clear.

1 - F /var/log/cron


Three. Summary

After the above steps, it is easy to set up a cron Job in Linux to do certain things periodically, such as deleting log and so on.

Four. References

The following references are very good, if you want to learn cron Job, should be enough.

1. Howto:add Jobs to cron under Linux or UNIX?


2. How to run a cron job using the sudo command


3. How to view crontab logging


4. Linux crontab Log Explanation


Novice small white, through the log to record their own learned, to stimulate, welcome criticism:-)

Best regards

Kevin Song





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.