{"id":1907,"date":"2017-12-04T09:48:39","date_gmt":"2017-12-04T17:48:39","guid":{"rendered":"http:\/\/blogs.msdn.microsoft.com\/commandline\/?p=1907"},"modified":"2019-02-25T20:47:23","modified_gmt":"2019-02-26T04:47:23","slug":"background-task-support-in-wsl","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/commandline\/background-task-support-in-wsl\/","title":{"rendered":"Background Task Support in WSL"},"content":{"rendered":"<p>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\u00a0 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.<\/p>\n<h3>Elevated and Non-Elevated WSL Instances<\/h3>\n<p>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 href=\"https:\/\/github.com\/Microsoft\/WSL\/issues\/626\">a discussion<\/a> from our GitHub repo. You can now run elevated and non-elevated WSL instances simultaneously without affecting the permissions in other sessions.<\/p>\n<h3>Startup Tasks<\/h3>\n<p>Any time we talk about running daemons in the background, someone inevitably asks if we support startup tasks (init, system, cloud init etc.).\u00a0 While WSL doesn&#8217;t support Linux startup tasks, now you can build custom startup scripts or other scheduled tasks using the Windows task scheduler, but that&#8217;s another post! We&#8217;re also investigating init-style functionality; keep sending us feedback.<\/p>\n<p><span><\/span><\/p>\n<h3>Background Task Walk-Through<\/h3>\n<p>Without further ado, let\u2019s 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 href=\"https:\/\/blogs.msdn.microsoft.com\/commandline\/2016\/06\/08\/tmux-support-arrives-for-bash-on-ubuntu-on-windows\/\">a blog<\/a> we wrote a while back.<\/p>\n<p>Let\u2019s start by creating a new tmux session and naming it:<\/p>\n<pre>$tmux new -s backgroundsession<\/pre>\n<p><img decoding=\"async\" width=\"600\" height=\"280\" class=\"alignnone size-medium wp-image-1915\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/pic1-600x280.png\" \/><\/p>\n<p>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.<\/p>\n<pre>$htop<\/pre>\n<p><img decoding=\"async\" width=\"600\" height=\"280\" class=\"alignnone size-medium wp-image-1925\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/pic2-600x280.png\" \/><\/p>\n<p>You should then see htop running in this tmux session.<\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"280\" class=\"alignnone size-medium wp-image-1935\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/pic3-600x280.png\" \/><\/p>\n<p>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.<\/p>\n<pre>$tmux ls\n<\/pre>\n<p><img decoding=\"async\" width=\"600\" height=\"280\" class=\"alignnone size-medium wp-image-1945\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/pic4-600x280.png\" \/><\/p>\n<p>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.<\/p>\n<pre>$tmux attach -t backgroundsession<\/pre>\n<p><img decoding=\"async\" width=\"600\" height=\"280\" class=\"alignnone size-medium wp-image-1946\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/pic5-600x280.png\" \/><\/p>\n<p>And now we can see our htop process has been running in the background!<\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"280\" class=\"alignnone size-medium wp-image-1955\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/pic6-600x280.png\" \/><\/p>\n<p>We hope you enjoy the new background task support in WSL!<\/p>\n<p>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 <a href=\"https:\/\/insider.windows.com\/en-us\/\">here<\/a>.<\/p>\n<p>Let us know how you use background tasks with WSL by submitting issues on our <a href=\"https:\/\/github.com\/Microsoft\/WSL\">GitHub repo<\/a> and Tweeting us at #WSL &#8211; @tara_msft and @virtualscooley.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0 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 [&hellip;]<\/p>\n","protected":false},"author":1032,"featured_media":4521,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5,9],"tags":[21,34,61,70,72],"class_list":["post-1907","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-10","category-bash-on-ubuntu-on-windows","tag-background-tasks","tag-daemons","tag-tmux","tag-windows-10","tag-wsl"],"acf":[],"blog_post_summary":"<p>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\u00a0 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts\/1907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/users\/1032"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/comments?post=1907"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts\/1907\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/media\/4521"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/media?parent=1907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/categories?post=1907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/tags?post=1907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}