清理脚本:/root/scripts/clean_mongodb_log.sh
#!/bin/bash # set -u echo "=== Started Time: `date` ===" mongodb_logdir=/var/log/mongodb days=7 pid=$(pidof $(which mongod)) if [ "X${pid}" != "X" ];then kill -SIGUSR1 $pid sleep 1m find $mongodb_logdir/ -type f | grep -v -x -f pattern.txt | xargs gzip find $mongodb_logdir/ -type f -mtime +$days -delete echo "clean mongodb log success!" else echo "clean mongodb log failure!" fi echo "=== Finished Time: `date` ===" # end
pattern.txt文件内容:
.*mongod.log$ .*gz$
定时清理任务:
00 00 * * * cd /root/scripts;./clean_mongodb_log.sh >/var/log/clean_mongodb.log 2>&1