首页 > 代码库 > linux下清理日志的脚本
linux下清理日志的脚本
需求:由于/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
本文出自 “梦女孩” 博客,请务必保留此出处http://dreamgirl1314.blog.51cto.com/1159474/1554726
linux下清理日志的脚本