Created
December 15, 2025 21:20
-
-
Save javimosch/a8c77efd17960fa6aa937156ed3f93ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ========================================== | |
| # Host-side auto memory fix for Umami container | |
| # Fully self-contained: | |
| # - detects container | |
| # - injects & runs memory-fix script inside container | |
| # - creates cron log | |
| # - installs itself to /usr/local/bin | |
| # - auto-adds to crontab if not present | |
| # ========================================== | |
| SCRIPT_NAME="umami_memory_fix_host.sh" | |
| CRON_LOG="/var/log/umami_memory_fix.log" | |
| INSTALL_PATH="/usr/local/bin/$SCRIPT_NAME" | |
| TMP_PATH="/tmp/fix-umami-memory.sh" | |
| CRON_SCHEDULE="0 2 * * *" | |
| # --- 1. Ensure cron log exists --- | |
| if [ ! -f "$CRON_LOG" ]; then | |
| touch "$CRON_LOG" | |
| chmod 644 "$CRON_LOG" | |
| echo "[INFO] Created cron log file at $CRON_LOG" | |
| fi | |
| # --- 2. Install self to /usr/local/bin if missing --- | |
| if [ "$0" != "$INSTALL_PATH" ] && [ ! -f "$INSTALL_PATH" ]; then | |
| cp "$0" "$INSTALL_PATH" | |
| chmod +x "$INSTALL_PATH" | |
| echo "[INFO] Installed script to $INSTALL_PATH" | |
| fi | |
| # --- 3. Auto-add to crontab if missing --- | |
| (crontab -l 2>/dev/null | grep -F "$INSTALL_PATH") || \ | |
| (crontab -l 2>/dev/null; echo "$CRON_SCHEDULE $INSTALL_PATH >> $CRON_LOG 2>&1") | crontab - | |
| echo "[INFO] Cron job ensured for daily run" | |
| # --- 4. Detect Umami container automatically --- | |
| CONTAINER_NAME=$(docker ps --format '{{.Names}} {{.Image}}' | grep 'ghcr.io/umami-software/umami:mysql' | awk '{print $1}') | |
| if [ -z "$CONTAINER_NAME" ]; then | |
| echo "[INFO] No Umami container found. Exiting." | tee -a "$CRON_LOG" | |
| exit 0 | |
| fi | |
| echo "[INFO] Detected Umami container: $CONTAINER_NAME" | tee -a "$CRON_LOG" | |
| # --- 5. Define in-container fix script --- | |
| read -r -d '' CONTAINER_SCRIPT << 'EOF' | |
| #!/bin/sh | |
| FLAG_FILE="/tmp/.memory_fix_applied" | |
| if [ -f "$FLAG_FILE" ]; then | |
| echo "[INFO] Memory fix already applied, skipping." | |
| exit 0 | |
| fi | |
| echo "[INFO] Stopping running Next.js processes..." | |
| NEXTJS_PIDS=$(ps -eo pid,command | grep '.next/CkLFzyF' | grep -v grep | awk '{print $1}') | |
| if [ -n "$NEXTJS_PIDS" ]; then | |
| echo "$NEXTJS_PIDS" | xargs kill -9 | |
| echo "[INFO] Killed PIDs: $NEXTJS_PIDS" | |
| else | |
| echo "[INFO] No running Next.js processes found" | |
| fi | |
| export NODE_OPTIONS="--max-old-space-size=384" | |
| echo "[INFO] NODE_OPTIONS set to $NODE_OPTIONS" | |
| echo "[INFO] Restarting Next.js server..." | |
| /app/.next/CkLFzyF -c /app/.next/gMvT -B & | |
| touch "$FLAG_FILE" | |
| echo "[INFO] Memory fix applied. Current running processes:" | |
| ps aux | grep '.next/CkLFzyF' | grep -v grep | |
| EOF | |
| # --- 6. Copy the script into the container --- | |
| echo "$CONTAINER_SCRIPT" | docker exec -i "$CONTAINER_NAME" sh -c "cat > $TMP_PATH && chmod +x $TMP_PATH" | |
| # --- 7. Run the script inside the container --- | |
| docker exec -i "$CONTAINER_NAME" sh "$TMP_PATH" | tee -a "$CRON_LOG" | |
| echo "[INFO] Memory fix applied in container $CONTAINER_NAME" | tee -a "$CRON_LOG" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment