Last active
August 30, 2025 18:53
-
-
Save xopez/1a737907e44dc876327f0149b31b06a2 to your computer and use it in GitHub Desktop.
brew autoupdate
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 | |
| # Pfad zu Homebrew (Apple Silicon / Intel) | |
| export PATH="/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin" | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| SCRIPT_PATH="${SCRIPT_DIR}/$(basename "${BASH_SOURCE[0]}")" | |
| PLIST_DIR="$HOME/Library/LaunchAgents" | |
| PLIST_NAME="com.user.brew-autoupdate.plist" | |
| PLIST_PATH="${PLIST_DIR}/${PLIST_NAME}" | |
| # Funktion zur Überprüfung der Internetverbindung (optimiert) | |
| check_internet() { | |
| # Homebrew-relevante Domains testen | |
| for domain in "github.com" "formulae.brew.sh" "raw.githubusercontent.com"; do | |
| if ping -c 1 -W 5 "$domain" &>/dev/null; then | |
| return 0 | |
| fi | |
| done | |
| return 1 | |
| } | |
| # Funktion zum Erstellen des Launch Agent | |
| create_launch_agent() { | |
| echo "Erstelle Launch Agent für automatische Updates..." | |
| # Erstelle LaunchAgents Verzeichnis falls es nicht existiert | |
| mkdir -p "$PLIST_DIR" | |
| # Erstelle plist Datei | |
| cat >"$PLIST_PATH" <<EOF | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>Label</key> | |
| <string>${PLIST_NAME%.plist}</string> | |
| <key>ProgramArguments</key> | |
| <array> | |
| <string>/bin/bash</string> | |
| <string>$SCRIPT_PATH</string> | |
| <string>--update-only</string> | |
| </array> | |
| <key>StartInterval</key> | |
| <integer>43200</integer> | |
| <key>RunAtLoad</key> | |
| <true/> | |
| <key>StandardOutPath</key> | |
| <string>/tmp/brew-autoupdate.log</string> | |
| <key>StandardErrorPath</key> | |
| <string>/tmp/brew-autoupdate.error.log</string> | |
| </dict> | |
| </plist> | |
| EOF | |
| # Lade Launch Agent | |
| launchctl unload "$PLIST_PATH" 2>/dev/null | |
| launchctl load "$PLIST_PATH" | |
| echo "Launch Agent erstellt und geladen:" | |
| echo " - Läuft bei jedem System-Start" | |
| echo " - Läuft alle 12 Stunden (43200 Sekunden)" | |
| echo " - Logs: /tmp/brew-autoupdate.log" | |
| echo " - Errors: /tmp/brew-autoupdate.error.log" | |
| } | |
| # Funktion zum Entfernen des Launch Agent | |
| remove_launch_agent() { | |
| if [ -f "$PLIST_PATH" ]; then | |
| echo "Entferne Launch Agent..." | |
| launchctl unload "$PLIST_PATH" 2>/dev/null | |
| rm "$PLIST_PATH" | |
| echo "Launch Agent entfernt." | |
| else | |
| echo "Kein Launch Agent gefunden." | |
| fi | |
| } | |
| # Funktion für das Homebrew Update | |
| run_brew_update() { | |
| echo "[$(date)] Überprüfe Internetverbindung..." | |
| # Warte maximal 5 Minuten auf Internetverbindung | |
| timeout=300 | |
| elapsed=0 | |
| while ! check_internet && [ $elapsed -lt $timeout ]; do | |
| echo "[$(date)] Keine Internetverbindung verfügbar. Warte 30 Sekunden... ($elapsed/$timeout)" | |
| sleep 30 | |
| elapsed=$((elapsed + 30)) | |
| done | |
| if [ $elapsed -ge $timeout ]; then | |
| echo "[$(date)] Timeout: Keine Internetverbindung nach 5 Minuten. Abbruch." | |
| exit 1 | |
| fi | |
| echo "[$(date)] Internetverbindung verfügbar. Starte Homebrew Auto-Update..." | |
| if command -v brew &>/dev/null; then | |
| echo "[$(date)] Führe brew update aus..." | |
| brew update | |
| echo "[$(date)] Führe brew upgrade aus..." | |
| brew upgrade | |
| echo "[$(date)] Führe brew cleanup aus..." | |
| brew cleanup | |
| echo "[$(date)] Homebrew Auto-Update abgeschlossen." | |
| else | |
| echo "[$(date)] Fehler: Homebrew ist nicht installiert oder nicht im PATH verfügbar." | |
| exit 1 | |
| fi | |
| } | |
| # Hauptlogik | |
| case "${1:-}" in | |
| --install-task) | |
| create_launch_agent | |
| ;; | |
| --remove-task) | |
| remove_launch_agent | |
| ;; | |
| --update-only) | |
| run_brew_update | |
| ;; | |
| --help | -h) | |
| echo "Verwendung: $0 [OPTION]" | |
| echo "" | |
| echo "Optionen:" | |
| echo " --install-task Installiert automatischen Task (bei Reboot + alle 12h)" | |
| echo " --remove-task Entfernt automatischen Task" | |
| echo " --update-only Führt nur das Update aus (für internen Gebrauch)" | |
| echo " --help, -h Zeigt diese Hilfe an" | |
| echo "" | |
| echo "Ohne Argument: Führt einmalig ein Update aus" | |
| ;; | |
| *) | |
| run_brew_update | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.