濮阳杆衣贸易有限公司

主頁 > 知識庫 > Linux使用shell腳本定時刪除歷史日志文件

Linux使用shell腳本定時刪除歷史日志文件

熱門標(biāo)簽:蘭州語音電銷機(jī)器人軟件 山東400電話如何辦理 高德地圖標(biāo)注在建線路 濟(jì)源電話外呼系統(tǒng)怎么樣 電銷機(jī)器人對公司貢獻(xiàn) 宿州防封外呼系統(tǒng)廠家 ai電銷機(jī)器人 如何開發(fā) 智能外呼系統(tǒng)如何部署 電話機(jī)器人服務(wù)差

1、tools目錄文件結(jié)構(gòu)

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

2、刪除歷史文件腳本 del_history_files

[root@www tools]# more tools/bin/del_history_files
#!/bin/sh
 
# 刪除指定目錄下,文件時間早于指定時間節(jié)點(diǎn)的文件,時間粒度:小時
# 配置文件格式 : 需清理的目錄=小時數(shù)
#
#
# define restricted path
PATH="/bin:/usr/bin:/sbin:/usr/sbin"
 
# adirname - return absolute dirname of 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、刪除歷史文件腳本的配置文件 del_history_files.cfg

[root@www tools]# more tools/etc/del_history_files.cfg
#需清理的目錄=小時數(shù)
/home/logs/nginx=720
/home/logs/varnish=720

4、crontab 執(zhí)行即可

[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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:巴中 衡水 南寧 晉中 佛山 安陽 云南 畢節(jié)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux使用shell腳本定時刪除歷史日志文件》,本文關(guān)鍵詞  Linux,使用,shell,腳本,定時,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux使用shell腳本定時刪除歷史日志文件》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux使用shell腳本定時刪除歷史日志文件的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    化州市| 邓州市| 吉木萨尔县| 理塘县| 博兴县| 达州市| 新巴尔虎右旗| 永嘉县| 尼勒克县| 皮山县| 仙桃市| 鄂伦春自治旗| 观塘区| 禹城市| 克山县| 时尚| 容城县| 三亚市| 华宁县| 北安市| 呼伦贝尔市| 伊宁市| 西乌珠穆沁旗| 宜宾市| 华容县| 广安市| 永寿县| 红河县| 灌阳县| 德化县| 大同县| 新竹市| 万宁市| 海城市| 南川市| 敖汉旗| 巴塘县| 台中县| 青冈县| 勃利县| 衢州市|