Linux 定时清理日志文件
1. 编写清理脚本
vi clearLog.sh
chmod +x clearLog.sh
clearLog.sh
内容:
#! /bin/sh
cd /home/user/log
echo -n "Clean up the log from seven days ago. " >> clearLog.log
date >> clearLog.log
find ./ -type f -mtime +7 -name "*.log" -exec rm -rf {} \;
2. 添加定时任务
cd /etc
crontab -e
追加一行内容(每天凌晨2点执行脚本)
0 2 * * * /home/user/clearLog.sh
重启 cron 服务
sudo service cron restart