宝塔管理的服务器如何清理磁盘空间,比如thinkphp生成的log文件以及runtime目录

  • 内容
  • 评论
  • 相关

1.在宝塔软件商店安装插件 宝塔WebHook 2.4

2.添加hook,脚本内容如下【脚本内容请结合实际情况处理】


#!/bin/bash
# 定义日志文件路径
LOG_FILE0="/dellogfile0.log"
LOG_FILE1="/dellogfile1.log"

function deleteFilesInDirectory() {
    local dir="$1"

    # 遍历文件夹中的所有文件和子文件夹
    for entry in "$dir"/*
    do
        lowercase_entry=$(echo "$entry" | tr '[:upper:]' '[:lower:]')
        if [[ "$lowercase_entry" == *"thinkphp"*  || "$lowercase_entry" == *"public/cnadmin"*  ]]; then
            continue
        fi

        if [ -d "$entry" ]; then
            echo "Deleting directory: $entry"
            echo "$(date) Deleting directory: $entry" >> $LOG_FILE0
            deleteFilesInDirectory "$entry"
            rmdir "$entry"
        elif [ -f "$entry" ]; then
            echo "Deleting file: $entry"
            echo "$(date) Deleting file: $entry" >> $LOG_FILE1
            rm "$entry"
        fi
    done
}

# 遍历www目录下的所有站点,清理里面的runtime和log文件夹,排除thinkphp目录里面的
for site_dir in /www/wwwroot/*/
do
    # 递归查找site_dir下的所有runtime和Runtime文件夹
    find "$site_dir" \( -type d -name runtime -o -type d -name Runtime \) | while read -r runtime_dir; do
        # 检查runtime或Runtime文件夹是否存在
        if [ -d "$runtime_dir" ]; then
            echo "Processing directory: $runtime_dir"
            deleteFilesInDirectory "$runtime_dir"
        fi
    done

    # 递归查找site_dir下的所有log文件夹
    find "$site_dir" -type d -name log | while read -r log_dir; do
        # 检查log文件夹是否存在
        if [ -d "$log_dir" ]; then
            echo "Processing directory: $log_dir"
            deleteFilesInDirectory "$log_dir"
        fi
    done
done
3.点击测试触发清理,或者通过webhook的url触发,或者使用宝塔的计划任务,定时执行上面shell脚本来实现


11.png

本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:宝塔管理的服务器如何清理磁盘空间,比如thinkphp生成的log文件以及runtime目录 - https://wlphp.com/?post=431

发表评论

电子邮件地址不会被公开。 必填项已用*标注