您的位置:首页 > 运维架构 > Shell

shell 脚本之清除日志记录

2013-07-23 14:41 393 查看
一个简单的清除系统日志和登录日志的shell 脚本
#!/bin/sh

#clean up system logs

LOG_DIR=/var/log
ROOT_UID=0 # When $UID=0, session have a root permission
LINES=50 # default save lines
E_XCD=66
E_NOTROOT=67

if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Must be root to run this script"
exit $E_NOTROOT
fi

if [ -n "$1" ]; then
lines=$1
else
lines=$LINES
fi

cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ]; then
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi
# if
# cd /var/log || {echo "Can't change to necessary directory." >2&
# exit E_XCD;}

tail -$lines messages >mesg.tmp

mv mesg.tmp messages

# cat /dev/null > messages
cat /dev/null > wtmp

echo "Logs cleaned up..."

exit 0

本文出自 “系统与数据库运维” 博客,请务必保留此出处http://laoheibao.blog.51cto.com/6380798/1255336
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: