Background Task Support in WSL


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.

Startup Tasks

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.

$htop

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.

$tmux ls

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.

Comments (14)

  1. L. says:

    What happens to WSL background tasks on (windows session) logoff? Are they killed, or not?

    1. When you logoff your Windows session, all Linux instances are terminated because they’re all running within the user’s session … which is now terminated.

    2. Stéphane BARIZIEN says:

      Good question.

      Leads to another question: is it possible to fire background task at Windows startup, before logon?

      1. Tara Raj says:

        Init tasks are not currently supported as explained in the “Startup Tasks” section above. We’re looking into this. You can, however, schedule tasks using the Windows task scheduler for now

      2. Keto Zhang says:

        That’s great. I no longer need to keep the Linux terminal open on a second desktop (so I won’t accidentally close it).

        Unfortunately this doesn’t change the fact that I can’t run SSHD on startup:
        sudo service ssh restart

        Without the need for sudo permission or editing visudo (specified here https://github.com/Microsoft/WSL/issues/612).

        1. You can use the Windows schedule task tool to start it, I believe. Also, OpenSSH is now available on the Windows side too.

          1. Keto Zhang says:

            It’s possible to use task schedule but you still have to edit visudo to allow “sudo service” otherwise you have to enter in your password.

            I tried OpenSSH with Powershell, it’s okay wish there was a lot more documentation. I can’t believe how hard it is to get to “ssh_config” with this OpenSSH.

  2. Iosif Karaioannoglou says:

    Does this mean crontab will work on windows?

    1. Kinda. You’ll still need to start the cron daemon manually, but it should keep running in the background after it’s been started, until you logout/restart your Windows session.

      1. Duke Best says:

        Will it be possible at some point to allow cron to start via a MSFT service? That’s how I enable cron via Cygwin and that’s a nice feature.

  3. Will says:

    Very cool! I can finally idle in IRC again.

  4. Ken says:

    Can I ask when these capabilities will reach those not on Insider? Specifically af_unix and the background stuff?

    1. I don’t have any dates to share, but note that:

      Insiders builds are ~weekly builds of the next version of Windows as it’s being built
      We’re aiming to release Windows ~twice a year -typically once in the spring and again in the fall/winter

  5. btipling says:

    Will this mean that ssh-agents will persist across windows sessions as well? That’s one of my biggest pain points personally. Thank you for this update!

Skip to main content