相信接觸過服務器維護或者應用管理的同窗都有被大量的服務器日誌困擾過吧,至少我是這樣,廢話少說~直接上腳本,很是簡單nginx
#!/bin/bash PARAM_SIZE=$# if [[ $PARAM_SIZE -lt 2 ]]; then echo "usage: $0 <log_dir> <pattern> [days]" echo " log_dir: the log directory" echo " pattern: match the log to delete" echo " days: n+1 days ago; defaut 0, before today's log." echo " $0 /var/log/nginx/access.log.* 3 : delete nginx's access log 4 days ago." fi LOG_DIR="$1" LOG_PATTERN="$2" if [[ $PARAM_SIZE -gt 2 ]]; then DAYS=$3 else DAYS=0 fi find $LOG_DIR -mtime +${DAYS} -name "$LOG_PATTERN" -exec rm -rvf {} \;
執行:./dellog.sh /var/log/nginx/access.log.* 1 bash
能夠結合crontab使用,自動清理日誌就完成了,真是一勞永逸的辦法服務器