Preventing WSL2 Auto-Shutdown¶
WSL2 has two independent timeout mechanisms that shut it down when idle. Both must be addressed for a persistent server.
The Two Timeouts¶
Instance Idle Timeout¶
- What: After the last user process in a distro exits, the distro instance shuts down.
- Default: 15 seconds (15000 ms).
- Config:
%UserProfile%\.wslconfigunder[general]:
VM Idle Timeout¶
- What: After ALL distro instances have terminated, the entire WSL2 VM shuts down.
- Default: 60 seconds (60000 ms).
- Config:
%UserProfile%\.wslconfigunder[wsl2]:
You need BOTH set to -1. Setting only vmIdleTimeout=-1 still allows the
distro instance to terminate after 15 seconds of no user processes. The VM stays
alive but your SSH server is gone.
Recommended .wslconfig¶
After editing, apply with: wsl --shutdown from PowerShell, then restart WSL.
Keep-Alive Processes (CRITICAL)¶
Do NOT rely on timeouts or systemd alone. wsl -- <command> exits after
the command returns and WSL2 shuts down ~15 seconds later. systemd keeping
the instance alive has known regressions
(WSL#13416).
The only reliable approach: a foreground sleep infinity process:
exec sleep infinity replaces the bash process with sleep, keeping wsl.exe
alive indefinitely. Use Start-Process -WindowStyle Hidden to hide the window.
This is what all our automation scripts use for both interactive tryout and the Task Scheduler persistent entry.
Known Bug: Services Suspended Despite -1 Timeout¶
GitHub issue microsoft/WSL#13291
reports that services (e.g., ollama, sshd) get suspended even with vmIdleTimeout=-1.
Workaround: also disable aggressive memory reclaim: