Linux uses shell scripts to regularly delete historical log files

Linux uses shell scripts to regularly delete historical log files

1. Tools directory file structure

[root@www tools]# tree tools/
tools/
├── bin
│ ├── del_history_files
│ 
└── etc
  ├── del_history_files.cfg
 
2 directories, 2 files

2. Delete history file script del_history_files

[root@www tools]# more tools/bin/del_history_files
#!/bin/sh
 
# Delete files in the specified directory whose file time is earlier than the specified time node. Time granularity: hours# Configuration file format: Directory to be cleaned = number of hours#
#
# define restricted path
PATH="/bin:/usr/bin:/sbin:/usr/sbin"
 
# adirname - return absolute dirname of a given file
adirname() { odir=`pwd`; cd `dirname $1`; pwd; cd "${odir}"; }
 
 
# ---------
# constants
# ---------
MYNAM=`basename "$0"`
MYDIR=`adirname "$0"`
MYCFG="${MYDIR}/../etc/${MYNAM}.cfg"
MYTMP="${MYDIR}/../tmp"
MYLCK="${MYTMP}/${MYNAM}.lock"
 
# perform some locking (as good as it gets in a shell)
[ -s "${MYLCK}" ] && kill -0 `cat "${MYLCK}"` 2>/dev/null &&
    die "${MYNAM}: already running!"
echo "$$" > "${MYLCK}"
 
PATHS=(`cat ${MYCFG}`)
for PP in ${PATHS[@]}
do
  APP_PATH=`echo ${PP} | awk -F'=' '{print $1}'`
  N=`echo ${PP} | awk -F'=' '{print $2}'`
    if [ -d ${APP_PATH} ] ; then
    T=`/bin/date --date "${N} hours ago" "+%Y%m%d%H%M"`
    TMP_FILE="/tmp/`echo ${PP} | md5sum | awk '{print $1}'`"
    touch -t ${T} ${TMP_FILE}
    find ${APP_PATH} ! -newer ${TMP_FILE} -type f -print0 | xargs -0 -n 100 rm -rf
    find ${APP_PATH} -type d -empty -print0 | xargs -0 -n 100 rm -rf &> /dev/null
    fi
done
 
rm -rf ${MYLCK}

3. Delete the configuration file del_history_files.cfg of the history file script

[root@www tools]# more tools/etc/del_history_files.cfg
#Directory to be cleaned = number of hours /home/logs/nginx=720
/home/logs/varnish=720

4. Run crontab

[root@www tools]# more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
#clear old logs
00 6 * * * root /home/tools/bin/del_history_files

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Script for regularly deleting files older than a certain day in Windows and Linux
  • Automatically back up Oracle databases using scripts in Linux and delete backups older than a specified number of days
  • Linux delete invalid link file script sharing
  • Ideas and tests for custom Linux scripts to prevent accidental deletion

<<:  React+axios implements github search user function (sample code)

>>:  Summary of MySQL's commonly used concatenation statements

Recommend

Vue implements a search box with a magnifying glass

This article shares with you how to use Vue to im...

Several situations that cause MySQL to perform a full table scan

Table of contents Case 1: Case 2: Case 3: To summ...

A simple method to implement Linux timed log deletion

Introduction Linux is a system that can automatic...

Detailed explanation of linux nslookup command usage

[Who is nslookup?] 】 The nslookup command is a ve...

Use pure CSS to create a pulsating loader effect source code

Effect Preview Press the "Click to Preview&q...

Zabbix uses PSK shared key to encrypt communication between Server and Agent

Since Zabbix version 3.0, it has supported encryp...

Methods and techniques for quickly displaying web page images

1. Use .gifs rather than .jpgs. GIFs are smaller ...

IE6 implements min-width

First of all, we know that this effect should be ...

Summary of the main attributes of the body tag

bgcolor="text color" background="ba...

Implementation of Nginx configuration Https security authentication

1. The difference between Http and Https HTTP: It...

A brief discussion on DDL and DML in MySQL

Table of contents Preface 1. DDL 1.1 Database Ope...

Detailed explanation of several ways to install CMake on Ubuntu

apt install CMake sudo apt install cmake This met...