Last active
August 9, 2025 05:23
-
-
Save gregberns/070e100251d07efd1e8af508d4e3903d 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/zsh | |
| # macOS Server Optimization Script | |
| # Disables unnecessary background processes and services for server use | |
| # Based on community research and tested configurations | |
| # WARNING: This script makes significant changes to your macOS system | |
| # Please read through and understand each section before running | |
| # Test in a non-production environment first | |
| set -e | |
| echo "=========================================" | |
| echo "macOS Server Optimization Script" | |
| echo "=========================================" | |
| echo "" | |
| echo "This script will:" | |
| echo "1. Disable unnecessary background services" | |
| echo "2. Configure power management for server use" | |
| echo "3. Disable visual effects and unnecessary features" | |
| echo "4. Optimize system settings for headless operation" | |
| echo "" | |
| echo "WARNING: This will make significant changes to your system!" | |
| echo "Make sure you understand what each section does." | |
| echo "" | |
| read "REPLY?Do you want to continue? (y/N): " | |
| if [[ "$REPLY" =~ ^[Yy]$ ]]; then | |
| echo "Continuing..." | |
| else | |
| echo "Aborting." | |
| exit 1 | |
| fi | |
| # Check if running as root for some commands | |
| if [[ $EUID -eq 0 ]]; then | |
| echo "Don't run this script as root. It will use sudo when needed." | |
| exit 1 | |
| fi | |
| # echo "" | |
| # echo "=========================================" | |
| # echo "STEP 1: Power Management Settings" | |
| # echo "=========================================" | |
| # # Configure power settings for server use | |
| # echo "Configuring power management for always-on operation..." | |
| # # Prevent sleep when on AC power | |
| # sudo pmset -c sleep 0 | |
| # sudo pmset -c disksleep 0 | |
| # sudo pmset -c displaysleep 0 | |
| # # Enable automatic restart after power failure | |
| # sudo pmset -c autorestart 1 | |
| # # Enable wake on network access | |
| # sudo pmset -c womp 1 | |
| # # Disable power nap | |
| # sudo pmset -c powernap 0 | |
| # # Disable standby and hibernate modes | |
| # sudo pmset -a standby 0 | |
| # sudo pmset -a hibernatemode 0 | |
| # # Set automatic wake/power on schedule (optional - daily at 7 AM) | |
| # # Uncomment the next line if you want scheduled daily startup | |
| # # sudo pmset repeat wakeorpoweron MTWRFSU 07:00:00 | |
| # echo "Power management configured for server operation." | |
| # echo "" | |
| # echo "=========================================" | |
| # echo "STEP 2: Disable Network Services" | |
| # echo "=========================================" | |
| # echo "Disabling WiFi and Bluetooth..." | |
| # # Disable WiFi (assuming ethernet connection) | |
| # sudo networksetup -setnetworkserviceenabled Wi-Fi off 2>/dev/null || echo "WiFi service not found or already disabled" | |
| # # Disable Bluetooth | |
| # sudo networksetup -setnetworkserviceenabled "Bluetooth PAN" off 2>/dev/null || echo "Bluetooth PAN not found or already disabled" | |
| # # Turn off Bluetooth hardware | |
| # sudo defaults write /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 0 | |
| # echo "Network services optimized." | |
| echo "" | |
| echo "=========================================" | |
| echo "STEP 3: Disable Spotlight Indexing" | |
| echo "=========================================" | |
| echo "Disabling Spotlight indexing..." | |
| # Disable Spotlight indexing on all volumes | |
| sudo mdutil -a -i off | |
| # Remove Spotlight from menu bar (user preference) | |
| defaults write com.apple.spotlight menuBarShowsSpotlight -bool false | |
| echo "Spotlight indexing disabled." | |
| echo "" | |
| echo "=========================================" | |
| echo "STEP 4: System UI Optimizations" | |
| echo "=========================================" | |
| echo "Optimizing system UI for headless operation..." | |
| # Disable visual effects | |
| # -- This doesnt seem to work | |
| # defaults write com.apple.universalaccess reduceTransparency -bool true | |
| # defaults write com.apple.universalaccess reduceMotion -bool true | |
| # Disable dock animations | |
| defaults write com.apple.dock launchanim -bool false | |
| defaults write com.apple.dock expose-animation-duration -float 0.1 | |
| # Disable window animations | |
| defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool false | |
| # Set solid color wallpaper (saves GPU resources) | |
| defaults write com.apple.desktop Background '{default = {ImageFilePath = "/System/Library/Desktop Pictures/Solid Colors/Stone.png"; };}' | |
| # Disable screen saver | |
| defaults -currentHost write com.apple.screensaver idleTime 0 | |
| # Disable automatic login window updates | |
| sudo defaults write /Library/Preferences/com.apple.loginwindow DisableScreenLock -bool true | |
| echo "UI optimizations applied." | |
| echo "" | |
| echo "=========================================" | |
| echo "STEP 5: Disable Unnecessary Launch Services" | |
| echo "=========================================" | |
| echo "NOTE: The following section requires System Integrity Protection (SIP) to be disabled." | |
| echo "To disable SIP:" | |
| echo "1. Reboot into Recovery Mode (Command+R)" | |
| echo "2. Open Terminal from Utilities menu" | |
| echo "3. Run: csrutil disable" | |
| echo "4. Reboot normally" | |
| echo "" | |
| read "REPLY?Has SIP been disabled? (y/N): " | |
| echo | |
| if [[ "$REPLY" =~ ^[Yy]$ ]]; then | |
| echo "Disabling unnecessary user launch agents..." | |
| # Array of user agents to disable (safe selections for server use) | |
| USER_AGENTS=( | |
| 'com.apple.ap.adprivacyd' | |
| 'com.apple.ap.promotedcontentd' | |
| 'com.apple.assistant_service' | |
| 'com.apple.assistantd' | |
| 'com.apple.BiomeAgent' | |
| 'com.apple.biomesyncd' | |
| 'com.apple.cloudd' | |
| 'com.apple.cloudpaird' | |
| 'com.apple.cloudphotod' | |
| 'com.apple.CoreLocationAgent' | |
| 'com.apple.financed' | |
| 'com.apple.gamed' | |
| 'com.apple.homed' | |
| 'com.apple.iCloudNotificationAgent' | |
| 'com.apple.iCloudUserNotifications' | |
| 'com.apple.intelligenceplatformd' | |
| 'com.apple.itunescloudd' | |
| 'com.apple.mediaanalysisd' | |
| 'com.apple.newsd' | |
| 'com.apple.parsecd' | |
| 'com.apple.passd' | |
| 'com.apple.photoanalysisd' | |
| 'com.apple.photolibraryd' | |
| 'com.apple.siriactionsd' | |
| 'com.apple.Siri.agent' | |
| 'com.apple.siriinferenced' | |
| 'com.apple.siriknowledged' | |
| 'com.apple.suggestd' | |
| 'com.apple.tipsd' | |
| 'com.apple.UsageTrackingAgent' | |
| 'com.apple.weatherd' | |
| ) | |
| for agent in "${USER_AGENTS[@]}"; do | |
| echo "Disabling user agent: $agent" | |
| launchctl bootout gui/501/${agent} 2>/dev/null || true | |
| launchctl disable gui/501/${agent} 2>/dev/null || true | |
| done | |
| echo "Disabling unnecessary system daemons..." | |
| # Array of system daemons to disable (conservative selection) | |
| SYSTEM_DAEMONS=( | |
| 'com.apple.analyticsd' | |
| 'com.apple.cloudd' | |
| 'com.apple.familycontrols' | |
| 'com.apple.GameController.gamecontrollerd' | |
| 'com.apple.locationd' | |
| 'com.apple.netbiosd' | |
| 'com.apple.wifianalyticsd' | |
| ) | |
| for daemon in "${SYSTEM_DAEMONS[@]}"; do | |
| echo "Disabling system daemon: $daemon" | |
| sudo launchctl bootout system/${daemon} 2>/dev/null || true | |
| sudo launchctl disable system/${daemon} 2>/dev/null || true | |
| done | |
| echo "Launch services optimization completed." | |
| else | |
| echo "Skipping launch services optimization. SIP must be disabled for this step." | |
| fi | |
| echo "" | |
| echo "=========================================" | |
| echo "STEP 6: Additional Optimizations" | |
| echo "=========================================" | |
| echo "Applying additional server optimizations..." | |
| # Disable crash reporting | |
| defaults write com.apple.CrashReporter DialogType none | |
| # Disable automatic software updates | |
| sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdate -bool false | |
| sudo defaults write /Library/Preferences/com.apple.SoftwareUpdate AutomaticCheckEnabled -bool false | |
| # Increase file descriptor limits for server applications | |
| echo 'kern.maxfiles=65536' | sudo tee -a /etc/sysctl.conf | |
| echo 'kern.maxfilesperproc=32768' | sudo tee -a /etc/sysctl.conf | |
| # Disable Game Center | |
| defaults write com.apple.gamed Disabled -bool true | |
| # Disable Notification Center | |
| launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist 2>/dev/null || true | |
| echo "Additional optimizations applied." | |
| echo "" | |
| echo "=========================================" | |
| echo "STEP 7: Create Restoration Script" | |
| echo "=========================================" | |
| echo "Creating restoration script..." | |
| cat > ~/restore_macos_defaults.sh << 'RESTORE_EOF' | |
| #!/bin/zsh | |
| echo "Restoring macOS default settings..." | |
| # Restore power management | |
| sudo pmset restoredefaults | |
| # Re-enable WiFi and Bluetooth | |
| sudo networksetup -setnetworkserviceenabled Wi-Fi on 2>/dev/null || true | |
| sudo defaults write /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 1 | |
| # Re-enable Spotlight | |
| sudo mdutil -a -i on | |
| defaults write com.apple.spotlight menuBarShowsSpotlight -bool true | |
| # Restore UI effects | |
| defaults write com.apple.universalaccess reduceTransparency -bool false | |
| defaults write com.apple.universalaccess reduceMotion -bool false | |
| defaults write com.apple.dock launchanim -bool true | |
| defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool true | |
| # Re-enable launch services (requires SIP to be disabled) | |
| echo "To restore launch services, delete the following files and reboot:" | |
| echo "sudo rm -f /private/var/db/com.apple.xpc.launchd/disabled.plist" | |
| echo "sudo rm -f /private/var/db/com.apple.xpc.launchd/disabled.501.plist" | |
| echo "" | |
| echo "Then restart your system." | |
| echo "Manual restoration steps completed. Restart required for full effect." | |
| RESTORE_EOF | |
| chmod +x ~/restore_macos_defaults.sh | |
| echo "Restoration script created at ~/restore_macos_defaults.sh" | |
| echo "" | |
| echo "=========================================" | |
| echo "OPTIMIZATION COMPLETE" | |
| echo "=========================================" | |
| echo "" | |
| echo "Summary of changes made:" | |
| echo "• Power management configured for always-on operation" | |
| echo "• WiFi and Bluetooth disabled (ethernet assumed)" | |
| echo "• Spotlight indexing disabled" | |
| echo "• Visual effects and animations minimized" | |
| echo "• Unnecessary background services disabled" | |
| echo "• System optimized for headless server operation" | |
| echo "" | |
| echo "IMPORTANT NOTES:" | |
| echo "• A restart is required for all changes to take effect" | |
| echo "• Keep SIP disabled if you want launch service changes to persist" | |
| echo "• Use ~/restore_macos_defaults.sh to restore defaults if needed" | |
| echo "• Monitor system performance and adjust as needed" | |
| echo "" | |
| echo "For manual fine-tuning, check running processes with:" | |
| echo " Activity Monitor or 'top' command" | |
| echo "" | |
| read -p "Restart now? (y/N): " -n 1 -r | |
| echo | |
| if [[ $REPLY =~ ^[Yy]$ ]]; then | |
| echo "Restarting in 10 seconds... (Ctrl+C to cancel)" | |
| sleep 10 | |
| sudo shutdown -r now | |
| else | |
| echo "Please restart manually to apply all changes." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment