No longer worry about huge Log Files: General log rolling script

Source: Internet
Author: User

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/164P62928-0.gif "style =" padding: 0px; margin: 0px; vertical-align: top; border: none; "alt =" "/> log_rotater.sh

#! /Bin/sh # Writed by yijian on 2012/7/22 # This script function: #1) implement a general log rolling by size #2) the log file extension must be ". log, otherwise, please slightly modify the code #3) support processing log files under multiple directories, if you need to use this function, # The dirs_list parameter must be enabled # Why do you need to write such a thing? # A: in daily life, the program output redirection is often found. # Or the Script output. If not processed, the # single file may be too large or even full of the entire disk; every script # repeatedly writes a log rolling logic, which is obviously boring. # Even some people are too lazy to handle it. After a long time, they will find a huge # log file, this script is intended to help solve this problem, so that everyone has no worries. # Usage: #1) copy the script to the directory where the log file is located, # Use nohup. /log_rotater.sh>/dev/null & other methods to start the script #2) In addition to the above method, you can also put log_rotater.sh under any directory, # But at this time, modify the value of backup_dir to the directory where the log file is located # sudo dpkg-reconfigure dash # The New Ubuntu version uses dash as shell by default. # This shell function is weak and does not support arrays, but the speed is fast. # You can use the following method to check the shell: # ls-l 'which Sh' # Modify the following parameters as neededBackup_count = 10 # Number of log scrolling backup_size = $ (1024*1024*200) # size of a single log file backup_interval = 60 # interval of detection, the Unit is seconds # if a file specified by dirs_list exists, the directory is read from dirs_list. # Otherwise, only a single directory specified by backup_dir is processed. # When a directory is added to a specified file in dirs_list, log_rotater.shbackup_dir =. # dirs_list =. /dirs. list # A directory is required for storing the files in the directory list. # Process log scrolling in a single directoryScan_single_dir () {dir = $ 1cd $ dirif test $? -Ne 0; thenreturnfi # eval $ (ls-l -- time-style = long-iso *. log 2>/dev/null | awk '{printf ("filesize = % s \ nfiledate = % s \ nfilename = % s \ n", $5, $6, $8);} ') if test $? -Ne 0; thenreturnfiif test-z $ filename; thenreturnfi if test $ filesize-gt $ backup_size; thenfile_index = $ ($ backup_count-1) while test $ file_index-gt 1; donew_filename = "$ {filename }. $ file_index "old_filename =" $ {filename }. $ ($ file_index-1) "if test-f" $ old_filename "; thenmv" $ old_filename "" $ new_filename "fifile_index = $ ($ file_index-1 )) done # Use truncate instead of mv here, # because the inode of the file must remain unchanged cp "$ filename" "$ {filename }. 1 "truncate-s 1024" $ filename "ficd-sleep 1} # Loop detectionWhile true; doif test! -F $ dirs_list; thenscan_single_dir $ backup_direlsewhile read dirpathdoscan_single_dir $ dirpathdone <$ dirs_listfi sleep $ backup_intervaldone

 

This article is from the "Fei Yue" blog, please be sure to keep this source http://mooon.blog.51cto.com/1246491/941086

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.