香港云主机最佳企业级服务商!

ADSL拨号VPS包含了中国大陆(联通,移动,电信,)

中国香港,国外拨号VPS。

当前位置:云主机 > 服务器资讯 >

电信ADSL拨号VPS
联通ADSL拨号VPS
移动ADSL拨号VPS

Linux使用shell脚本定时删除历史日志文件


时间:2020-10-27 10:57 作者:admin610456


1、tools目录文件结构

[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 # 删除指定目录下,文件时间早于指定时间节点的文件,时间粒度:小时# 配置文件格式 : 需清理的目录=小时数### define restricted pathPATH="/bin:/usr/bin:/sbin:/usr/sbin" # adirname - return absolute dirname of given fileadirname() { 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    fidone rm -rf ${MYLCK}

3、删除历史文件脚本的配置文件 del_history_files.cfg

[root@www tools]# more tools/etc/del_history_files.cfg#需清理的目录=小时数/home/logs/nginx=720/home/logs/varnish=720

4、crontab 执行即可

[root@www tools]# more /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/#clear old logs00 6 * * * root /home/tools/bin/del_history_files

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

(责任编辑:admin)






帮助中心
会员注册
找回密码
新闻中心
快捷通道
域名登录面板
虚机登录面板
云主机登录面板
关于我们
关于我们
联系我们
联系方式

售前咨询:17830004266(重庆移动)

企业QQ:383546523

《中华人民共和国工业和信息化部》 编号:ICP备00012341号

Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究

云官方微信

在线客服

  • 企业QQ: 点击这里给我发消息
  • 技术支持:383546523

  • 公司总台电话:17830004266(重庆移动)
  • 售前咨询热线:17830004266(重庆移动)