需求:由于/var分区单独划分,给的分区大小才5G,日志写满了,需要写个脚本定时清理日志文件
#!/bin/bash
##delete log script ,write by xl
LOG_DIR1=/var/log
LOG_DIR2=/var/spool/mail
LOG_DIR3=/var/spool/mqueue
ROOT_UID=0
##Must be use root to run this script
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit 1
fi
cd $LOG_DIR1 || {
echo "Can not change to necessary dircectory." >&2
exit 1
}
#把空设备重定向到文件里面
cat /dev/null > maillog && echo "Logs cleaned up."
cat /dev/null > maillog.1 && echo "Logs cleaned up."
cat /dev/null > maillog.2 && echo "Logs cleaned up."
cd $LOG_DIR2 || {
echo "Can not change to necessary dircectory." >&2
exit 1
}
cat /dev/null > root && echo "Logs cleaned up."
cat /dev/null > stock && echo "Logs cleaned up."
###清除mqueue下的小文件
cd $LOG_DIR3
ls |xargs rm -rf
本文转自 xxl714 51CTO博客,原文链接:http://blog.51cto.com/dreamgirl1314/1554726,如需转载请自行联系原作者