echo '* */3 * * * /root/clearLog.sh' >> /var/spool/cron/root
cat <<EOF>/root/clearLog.sh
#!/bin/bash
removeDirLog() {
# 检查是否提供了第一个参数
if [ -z "$1" ]; then
echo "Error: First argument is missing."
return 1 # 返回一个非零退出码表示错误
fi
# 检查是否提供了第二个参数
if [ -z "$2" ]; then
echo "Error: Second argument is missing."
return 1
fi
find $1 -name "*.log" -type f -mtime +$2 -exec rm {} \;
}
clearDirLog() {
echo "==================== start clean nginx logs =========================="
# 检查是否提供了第一个参数
if [ -z "$1" ]; then
echo "Error: First argument is missing."
return 1 # 返回一个非零退出码表示错误
fi
logs=$(find $1 -name *.log)
for log in $logs; do
echo "clean logs : $log"
cat /dev/null >$log
done
echo "==================== end clean nginx containers logs =========================="
}
#clearDirLog /usr/local/nginx/logs/
removeDirLog /var/log 3
removeDirLog /data/logs 3
EOF
chmod a+x /root/clearLog.sh