Starting in Windows Insiders Build 17046, WSL supports background tasks (including daemons). In the past, if you opened WSL and started sshd, httpd, screen, or tmux you needed to have a console window open to keep those tools running. But, starting with 17046, these processes will continue running in the background even after the last console window has been closed.
Elevated and Non-Elevated WSL Instances
As part of background process support, we have incorporated your asks for a better experience while running multiple WSL instances (WSL in 2+ console windows) as per a discussion from our GitHub repo. You can now run elevated and non-elevated WSL instances simultaneously without affecting the permissions in other sessions.
Any time we talk about running daemons in the background, someone inevitably asks if we support startup tasks (init, system, cloud init etc.). While WSL doesn't support Linux startup tasks, now you can build custom startup scripts or other scheduled tasks using the Windows task scheduler, but that's another post! We're also investigating init-style functionality; keep sending us feedback.
Background Task Walk-Through
Without further ado, let’s take a walk-through background task support using tmux. If you are not already familiar with tmux, it is a terminal multiplexer command line tool that allows you to split terminal screens and toggle between windows rather seamlessly. For more tips on using tmux, please refer to a blog we wrote a while back.
Let’s start by creating a new tmux session and naming it:
$tmux new -s backgroundsession
I am going to run htop in this background task. To do the same you will need to have htop installed (sudo apt-get install htop). Htop is an interactive system-monitor process viewer meaning it lists all the processes currently running on your machine.
You should then see htop running in this tmux session.
Now that I have a task running in WSL, I can close the window without the session terminating. Go ahead and close the window and open up a new one to check which tmux sessions are running.
You can see the tmux session with htop has been and is still running in the background. To pull up the session we will elevate it through a process called attaching . Note you can conversely use detach to de-elevate a session. Attaching a session will allow us to view it in our terminal window.
$tmux attach -t backgroundsession
And now we can see our htop process has been running in the background!
We hope you enjoy the new background task support in WSL!
And a big thank you to our Windows Insiders for helping us find issues early and for your feedback. If you want to access our early-preview features like this one, please join the Insider program! You can find details on joining here.
Let us know how you use background tasks with WSL by submitting issues on our GitHub repo and Tweeting us at #WSL - @tara_msft and @virtualscooley.