保留文件清理日志方式:

  1. >msg.log

  2. cat dev/null>msg.log

  3. echo “ ”>msg.log


shell脚本例子:

  1. root用户的$UID固定为0:

[root@localhost ~]# echo $UID

0

2.清理日志的脚本:

#!/bin/sh

path=/var/log

myuid=0

if [ $UID -ne $myuid ];then

   echo "must be root to run"

   exit 1

else

   cd $path ||{

    echo "cannot change to directory" >/dev/null 2>&1

    exit 1

    }

fi

cat /dev/null>err.log

echo "log is clean.'

exit 0 #退出之前返回0表示成功,返回1表示失败