Chinaunix首页 | 论坛 | 博客
  • 博客访问: 465648
  • 博文数量: 65
  • 博客积分: 2645
  • 博客等级: 少校
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-08 17:04
文章分类

全部博文(65)

文章存档

2010年(5)

2009年(5)

2008年(14)

2007年(35)

2006年(6)

分类:

2010-03-22 20:08:56

保存一個自動刪日志的shell腳本 :)
~~~~~~~~~~~~~~~~~~~~~~~~~ begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/sh
# FileName: delete_log.sh
# 作用: 刪除最后修改時間在 n 天前的日志文件. 清 Mail
# 備注: 在 crontab 中添加: 0 6 * * * . $HOME/.profile; sh $HOME/tools/delete_log.sh
# $Id:$


# 應用程序目錄
app_bin_dir="~/bin"

# 應用程序日志目錄
app_log_dir="~/log"

# 文件最后修改時間
n=1

# 刪除最后修改時間為 n 天前(不含 n 本身)的文件
# $1: 文件所在目錄
# $2: 文件名
delete_files(){
file_dir=$1
file_name=$2

find ${file_dir} -type f -name "${file_name}" -mtime +${n} -exec rm {} \;
}

# 刪除 xx 的日志
delete_files ${app_log_dir} "xx*.log"

# 刪 其它日志
delete_files ${app_log_dir} "other.*.log"

# 清 mail
echo "" > /var/mail/`whoami`
~~~~~~~~~~~~~~~~~~~~~~~~~ over ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
腳本名: delete_log.sh
在 crontab 中添加如下信息:
0 6 * * * . $HOME/.profile; sh $HOME/tools/delete_log.sh 1>/dev/null 2>&1
其中:
(a) ". $HOME/.profile" 作用為 執行 .profile, 加載環境變量. 
(b) "1>/dev/null 2>&1" 含義:將“標準錯誤輸出”重定向到 “標準輸出”,將“標準輸出”重定向到文件 /dev/null (注1)中。(即,stdout、stderr的信息不在屏幕上顯示,也不寫入mail中)

注1:
In Unix-like operating systems, /dev/null or the null device is a special file that discards all data written to it (but reports that the write operation succeeded), and provides no data to any process that reads from it (yielding EOF immediately).
-- http://en.wikipedia.org/wiki//dev/null

阅读(3230) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

安何2010-05-25 19:10:11