结论与定位
更稳妥的自动清理方案
使用 logrotate 做日志轮转(系统自带、长期维护推荐)
/var/log/myapp/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 root adm
sharedscripts
postrotate
/bin/kill -HUP $(cat /var/run/syslogd.pid 2>/dev/null) 2>/dev/null || true
endscript
}
清理 journald 系统日志(systemd 系统)
用 cron 定期删除应用日志(适合非 systemd 管理的日志或自定义目录)
0 2 * * * find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;
find /var/log -type f -name "*.log" -size +50M -mtime +7 -exec truncate -s 0 {} \;
安全与维护建议