避免Mac休眠后VS Code Remote需重新加载窗口的方案
Great question! I've run into this exact issue with VS Code Remote SSH on my Mac too. Let's break down the lowest-power solutions to avoid those annoying reconnection waits, ordered by energy efficiency:
1. Keep SSH Sessions Alive (Lowest Energy Impact)
This is the best approach because it doesn't alter your Mac's sleep behavior at all—it just ensures the SSH connection stays active so you don't have to reload the VS Code window when your Mac wakes up.
Configure SSH Heartbeats
Add these lines to your ~/.ssh/config file (create it if it doesn't exist):
Host * ServerAliveInterval 30 ServerAliveCountMax 3
ServerAliveInterval 30: Sends a keep-alive packet to the remote server every 30 seconds, preventing the server from timing out the idle connection.ServerAliveCountMax 3: If the server doesn't respond to 3 consecutive packets, it closes the connection (prevents hanging, unresponsive sessions).
VS Code Remote SSH Specific Settings
You can also configure this directly in VS Code to override system SSH settings for remote sessions:
- Open VS Code Settings (
Cmd+,). - Search for
remote.SSH.serverAliveInterval. - Set it to
30(matches the SSH config above). - Optional: Adjust
remote.SSH.connectTimeoutif you need longer wait times for reconnection after waking.
With this setup, your SSH session will stay active even if your Mac goes to sleep. When you wake it up, VS Code will reconnect instantly without reloading the entire window.
2. Temporarily Disable Idle Sleep Only During Active VS Code Remote Sessions
If the SSH keep-alive approach doesn't work (e.g., your remote server enforces strict connection timeouts that can't be bypassed), you can use caffeinate—a built-in Mac command—to prevent idle sleep only when you have an active VS Code Remote SSH window open. This uses slightly more energy than the first method but way less than disabling sleep entirely.
Automatic Script to Toggle Caffeinate
Create a simple bash script that checks for active VS Code remote sessions and starts/stops caffeinate accordingly:
#!/bin/bash while true; do # Check if VS Code has any remote SSH windows open if pgrep -f "code.*remote-ssh" > /dev/null; then # Start caffeinate -i if it's not already running (prevents idle sleep) if ! pgrep -f "caffeinate -i" > /dev/null; then caffeinate -i & echo "Caffeinate started (preventing idle sleep)" fi else # Stop caffeinate if no remote sessions are active pkill -f "caffeinate -i" echo "Caffeinate stopped (allowing idle sleep)" fi sleep 60 # Check every minute done
Make the Script Run on Startup
- Save the script as
vscode_remote_keepawake.shin your~/Scriptsfolder (create the folder if needed). - Make it executable:
chmod +x ~/Scripts/vscode_remote_keepawake.sh. - Add it to your Mac's login items:
- Open System Settings > General > Login Items.
- Click the
+button, navigate to the script, and add it.
What caffeinate -i Does
- It only prevents your Mac from entering idle sleep—you can still manually put your Mac to sleep anytime.
- It doesn't keep your screen on (you can still set your display to turn off after inactivity).
- It uses minimal extra energy compared to disabling sleep entirely.
Key Notes
- The SSH keep-alive method is always preferred for lowest energy use.
- If you use the
caffeinatemethod, remember it will only prevent idle sleep—your Mac will still sleep if you close the lid or manually trigger sleep.
内容的提问来源于stack exchange,提问作者Janosh

