You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

避免Mac休眠后VS Code Remote需重新加载窗口的方案

Low-Energy Fixes for VS Code Remote SSH Session Drops on Mac (Without Disabling Hibernate Entirely)

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:

  1. Open VS Code Settings (Cmd+,).
  2. Search for remote.SSH.serverAliveInterval.
  3. Set it to 30 (matches the SSH config above).
  4. Optional: Adjust remote.SSH.connectTimeout if 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

  1. Save the script as vscode_remote_keepawake.sh in your ~/Scripts folder (create the folder if needed).
  2. Make it executable: chmod +x ~/Scripts/vscode_remote_keepawake.sh.
  3. 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 caffeinate method, remember it will only prevent idle sleep—your Mac will still sleep if you close the lid or manually trigger sleep.

内容的提问来源于stack exchange,提问作者Janosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:37:47