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
最后修改:2024 年 05 月 11 日
如果觉得我的文章对你有用,请随意赞赏