KUK
Server: Apache
System: Linux 247.44.167.72.host.secureserver.net 5.14.0-611.49.2.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Apr 30 09:05:08 EDT 2026 x86_64
User: root (0)
PHP: 8.0.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/nydus/bin/cleanup-mei.sh
#!/usr/bin/bash
# Clean orphaned PyInstaller _MEI* directories from /opt/nydus/tmp
# Only removes directories older than 2 hours with no active processes
#
# This script is designed to be called:
# - On service startup (via ExecStartPre)
# - Periodically via systemd timer (every 6 hours)
#
# Safety checks:
# - Only removes directories older than MAX_AGE_MINUTES
# - Uses fuser/lsof to efficiently check if directory is in use
# - Logs all cleanup actions to syslog

# Don't use set -e: cleanup is best-effort and should never block service start
set +e

TMP_DIR="${NYDUS_TMP_DIR:-/opt/nydus/tmp}"
MAX_AGE_MINUTES="${NYDUS_MEI_MAX_AGE_MINUTES:-120}"  # 2 hours default

# Exit if tmp directory doesn't exist
if [ ! -d "$TMP_DIR" ]; then
    exit 0
fi

# Check if a _MEI directory is in use by any process
# Returns 0 if in use, 1 if not in use
is_mei_in_use() {
    local mei_dir="$1"

    # Use fuser for fast check (single syscall vs iterating all /proc entries)
    if command -v fuser >/dev/null 2>&1; then
        fuser -s "$mei_dir" 2>/dev/null && return 0
        return 1
    fi

    # Fallback to lsof if fuser not available
    if command -v lsof >/dev/null 2>&1; then
        lsof +D "$mei_dir" >/dev/null 2>&1 && return 0
        return 1
    fi

    # If neither tool available, assume not in use (safe for cleanup)
    return 1
}

# Find and clean stale _MEI* directories
find "$TMP_DIR" -maxdepth 1 -type d -name '_MEI*' -mmin +"$MAX_AGE_MINUTES" 2>/dev/null | while read -r dir; do
    # Check if any process is using this directory
    if ! is_mei_in_use "$dir"; then
        if rm -rf "$dir" 2>/dev/null; then
            logger -t nydus-cleanup "Cleaned orphaned PyInstaller dir: $dir"
        fi
    else
        logger -t nydus-cleanup "Skipping in-use PyInstaller dir: $dir"
    fi
done

exit 0