Linux使用shell脚本定时删除历史日志文件
1、tools目录文件结构
[root@wwwtools]#treetools/ tools/ ├──bin │├──del_history_files │ └──etc ├──del_history_files.cfg 2directories,2files
2、删除历史文件脚本del_history_files
[root@wwwtools]#moretools/bin/del_history_files
#!/bin/sh
#删除指定目录下,文件时间早于指定时间节点的文件,时间粒度:小时
#配置文件格式:需清理的目录=小时数
#
#
#definerestrictedpath
PATH="/bin:/usr/bin:/sbin:/usr/sbin"
#adirname-returnabsolutedirnameofgivenfile
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"
#performsomelocking(asgoodasitgetsinashell)
[-s"${MYLCK}"]&&kill-0`cat"${MYLCK}"`2>/dev/null&&
die"${MYNAM}:alreadyrunning!"
echo"$$">"${MYLCK}"
PATHS=(`cat${MYCFG}`)
forPPin${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}hoursago""+%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}-typef-print0|xargs-0-n100rm-rf
find${APP_PATH}-typed-empty-print0|xargs-0-n100rm-rf&>/dev/null
fi
done
rm-rf${MYLCK}
3、删除历史文件脚本的配置文件del_history_files.cfg
[root@wwwtools]#moretools/etc/del_history_files.cfg #需清理的目录=小时数 /home/logs/nginx=720 /home/logs/varnish=720
4、crontab执行即可
[root@wwwtools]#more/etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ #clearoldlogs 006***root/home/tools/bin/del_history_files
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。