| Server IP : 68.178.202.69 / Your IP : 216.73.216.43 Web Server : Apache System : Linux 69.202.178.68.host.secureserver.net 3.10.0-1160.139.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Nov 3 13:30:41 UTC 2025 x86_64 User : ikioworld ( 1005) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /opt/nydus/bin/ |
Upload File : |
#!/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
#
# PyInstaller processes may not have open file handles to the _MEI directory
# because files are loaded into memory and handles are closed. We need to check:
# 1. Open file handles (fuser/lsof) - may be empty for PyInstaller
# 2. Memory-mapped files (/proc/*/maps) - shared libraries are mmap'd
# 3. Environment variables (/proc/*/environ) - PyInstaller sets _MEIPASS2
is_mei_in_use() {
local mei_dir="$1"
local mei_basename
mei_basename=$(basename "$mei_dir")
# Check 1: Use fuser for fast check on open file handles
if command -v fuser >/dev/null 2>&1; then
if fuser -s "$mei_dir" 2>/dev/null; then
return 0
fi
fi
# Check 2: Look for _MEIPASS2 environment variable pointing to this directory
# This is the most reliable check for PyInstaller processes
if grep -q "$mei_basename" /proc/*/environ 2>/dev/null; then
return 0
fi
# Check 3: Check memory-mapped files (shared libraries loaded from _MEI)
if grep -q "$mei_basename" /proc/*/maps 2>/dev/null; then
return 0
fi
# Check 4: Fallback to lsof if available
if command -v lsof >/dev/null 2>&1; then
if lsof +D "$mei_dir" >/dev/null 2>&1; then
return 0
fi
fi
# No process is using this directory
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