tomcat, nginx logs regularly clean up

1. Crontab Timing task

   Crontab basic grammar

  t1 t2 t3 t4 t5 program
  • Where t1 is a minute, t2 for hours, t3 represents a month in the first few days, t4 represents the month, t5 represents the day of the week. program represents a program to be executed.
  • Represents be performed every minute program, t2 * indicates every hour for the execution of the program, the rest by analogy when t1 is *
  • B represents the minutes of this time to be executed from a minute, t2 is a diagram to be executed from a first ab b hours, the rest by analogy when t1 is ab
  • N represents each of a minute time intervals, t2 is * / n n represent each one hour time intervals, the rest by analogy when t1 is * / n
  • Denotes a, b, c ... is the time t1 when a, b, c, ... represents when a, b, c, ... minutes to perform, t2 is a, b, c, ... hours to perform, the rest by analogy

2. Tomcat log cleaning script

  With the project running Tomcat log long more and more likely to hold over large hard disk access. Below posted a personal use Tomcat log cleaning script.

. 1 #! / Bin / SH 
2  
. 3  # log file directory
 . 4 # = path / usr / local / Tomcat / Apache-tomcat- 8.5 . 32 / logs
 . 5 path = / usr / local / Tomcat / Apache-tomcat- 8.5 . 32 / logs
 . 6  
. 7  CD $ path {}
 . 8  
. 9  # log backups catalina.out
 10  # 7 days prior to the date of acquisition
 . 11 bak_date = ` dATE + M-%%% Y-D -d " . 7 days ago Member " `
 12 is  # catalina.out backup log, added after the date
 13 is # CP catalina.out catalina.out. bak_date} {$ .log
14  # catalina.out empty log file
 15  echo > catalina.out
 16  
. 17  # 7 days before deleting the log file
 18  # 7 days prior to the date of acquisition
 . 19 del_date = ` DATE + M-%%% Y-D -d " . 7 ago Member Days " `
 20 is  # acquires date string file name, then this time the corresponding operation, localhost_access_log suffix generally txt file, here comprising a txt file
 21 is  for n- in ` LS * .log * .txt - . 1 `; do 
22 is      m =` echo $ n-| awk . -F ' {Print $ (of NF-. 1)} ' `
 23 is      m =` echo ${m:0-10}`
24     if [ ! $m ]; then
25         echo "IS NULL"
26         continue
27     fi
28     if [[ $m < $del_date || $m = $del_date ]];then
29         echo file $n will be deleted.
30         rm -rf $n
31     fi
32 done
Tomcat Cleanup Script

3. Nginx logs cut, clean-up script

  Nginx different Tomcat log, not the timing of cutting the log, the log will be placed in access.log and error.log. When troubleshooting a problem, it will be very convenient. Below posted a personal use Nginx logs cut clean-up script.

1 ! # / Bin / bash
 2  
3  #nginx error log backups
 4  cp /usr/local/nginx/logs/error.log / usr / local / nginx / logs / error - $ ( DATE -d " Yesterday " + " % Y-M-% D% " ) .log
 . 5  CAT / dev / null > / usr / local / nginx / logs / the error.log
 . 6  
. 7  # nginx the access log backup
 . 8  CP / usr / local / nginx / logs /access.log / usr / local / Nginx / logs / Access - $ ( DATE -d " Yesterday " + " % Y-M-% D% " ) .log
 . 9 CAT / dev / null > / usr / local / Nginx / logs / the access.log
 10  
. 11  # clear log backup file is 3 days before
 12 is  Find / usr / local / Nginx / logs / -mtime + . 3 -type F -name \ .log * | xargs  RM -f
Nginx cutting, cleaning logs

4. Crontab Timing task of formulating

  Crontab syntax details, please refer to Baidu -

# Interface written into the crontab 
crontab - E 

# crontab execute written statement, nginx here to clean up the script, for example 
# / Home / crontab / clean_nginx_log.sh is my script location, next to replace
 1  0 * * * / bin / SH / Home . / crontab / clean_nginx_logs SH >> /opt/cut_nginx_log.log 2 > & 1 

# save and exit start 
: WQ 
Service crond start
Written Crontab

 

Guess you like

Origin www.cnblogs.com/xiaobingblog/p/11419243.html