Does WSL Keep Running After Closing the Terminal?
Exploring WSL’s Behavior and How to Keep It Running
If you’re a Windows 11 user and working with WSL (Windows Subsystem for Linux)—specifically with the Ubuntu distribution—you’ve probably wondered:
"When I close my terminal window, does my Ubuntu environment keep running?"
The short answer is: No, it does not keep running by default. But there’s a lot more you should know. Let’s dive into it.
How WSL Works
When you open WSL by typing wsl
or launching it through Windows Terminal, it starts an instance of your chosen Linux distribution (in your case, Ubuntu). This instance is similar to a lightweight virtual machine, and it initializes only when you start it.
While your terminal window is open:
- Your WSL instance is active.
- All Linux processes you run (for example, web servers, background scripts) are alive and running.
However, when you close the terminal window (or the console session where WSL is running):
- WSL stops the active instance.
- All running processes are terminated—unless you specifically configured them to run detached.
But Why Doesn’t It Keep Running?
This behavior is intentional.
- WSL is designed to be lightweight and start on-demand.
- Closing the terminal effectively ends the user session, signaling WSL to shut down the instance.
- Unlike a traditional Linux server, WSL doesn’t keep background daemons running indefinitely.
However, with the latest WSL2, the subsystem enters a "light hibernation" state, allowing for faster startup next time. But this hibernation does not keep your processes running—they’ll need to be restarted when you open WSL again.
How Can You Keep WSL Running?
If you want your processes to continue running even after you close the terminal, you have a few options:
1️⃣ Use tmux
or screen
These are terminal multiplexers that:
- Let you start processes inside a detachable session.
- Even if you close the terminal, you can reattach later and resume where you left off.
Example steps:
sudo apt update && sudo apt install tmux
tmux new -s mysession
# Run your commands here
# Detach: Ctrl+b then d
# Reattach later: tmux attach -t mysession
2️⃣ Run Processes in the Background
You can run a command with &
to send it to the background:
my-long-running-script.sh &
However, if you close the terminal, the background process is still tied to the session and will stop.
For true independence, use nohup
:
nohup my-long-running-script.sh > output.log 2>&1 &
This allows it to persist beyond the terminal session.
3️⃣ Leverage WSL’s systemd
Support
In recent versions of WSL2, systemd support is available:
- You can enable
systemd
in/etc/wsl.conf
to manage background services. - This lets you configure services that start automatically with WSL and continue running in the background, similar to a full Linux system.
Here’s a simplified/etc/wsl.conf
:
[boot]
systemd=true
Restart WSL using:
wsl --shutdown
Now, you can use systemctl
to manage services.
4️⃣ Windows Task Scheduler or Background Scripts
You can set up a Windows Scheduled Task that:
- Starts WSL and runs a specific command/script at boot or on a schedule.
This way, you can trigger background tasks automatically, even without a terminal window.
Important Considerations
✅ WSL is not a persistent environment like a dedicated Linux server. It’s designed for development and light server tasks, not continuous uptime.
✅ Using WSL for production services is generally discouraged—for long-running server tasks, a VM or a dedicated server is preferable.
✅ Hibernation or fast start in WSL2 only affects startup speed, not process persistence.
✅ Networking issues may arise if your process depends on ports. When WSL stops, those ports become unavailable.
Finally
To summarize:
- No, your Ubuntu environment in WSL does not keep running when you simply close the terminal.
- But with tools like tmux, nohup, systemd, or a Windows Scheduled Task, you can keep processes running independently.
- It’s worth understanding how WSL behaves so you can design your workflows accordingly.
Comments ()