Automatically Start WSL SSH and Various Services on Windows

Mike P. Sinn
3 min readAug 2, 2021

--

Let’s automate the WSL or WSL2 SSH and services startup process!

In Windows Explorer, this in the address bar and press enter to go to your home folder:

%USERPROFILE%

Create this Visual Basic Script file called wsl_start.vbs

' Start services
Set UAC = CreateObject("Shell.Application")
command = "/c """ + CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) + "\control.bat"" start"
UAC.ShellExecute "C:\Windows\System32\cmd.exe", command, "", "runas", 0
Set UAC = Nothing

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\Windows\System32\bash.exe -c /mnt/c/Development/qm-api/scripts/wsl/wsl_start.sh",0
Set WshShell = Nothing

Also, create this shell script in your home folder called wsl_start.sh:

#!/bin/bash
sudo service ssh --full-restart
# Add or remove below according to your own sevicessudo service mysql restart || true
sudo service redis-server restart || true
sudo service nginx restart || true

Open Task Scheduler:

  1. Press “⊞ Windows”
  2. Enter “Task Scheduler” into the search bar
  3. Click “Task Scheduler”

Create the Task:

  1. Click “Create Basic Task” in the right panel
  2. Copy the text from below these instructions
  3. Paste the text into the “Name” text field
  4. Click “Next”
  5. Select “When the Computer Starts”
  6. Click “Next”
  7. Select “Start a Program”
  8. Click “Next”
Windows Subsystem for Linux 2 (WSL2)

Specify the Action

  1. Copy the path from below these instructions
  2. Paste this into the “Program/Script” field
%USERPROFILE%\wsl_startup.vbs
  1. Click “Next”
  2. Click “Yes”
  3. Check “Open the Properties Dialog…”
  4. Click “Finish”

Specify Trigger

  1. Click “Triggers” in the top tab bar and select “At Startup”
  2. Click “Edit”
  3. Check “Delay Task For” and enter “30 Seconds”
  4. Click “OK”

Run in the Background

  1. Click “General” in the tab bar
  2. Select “Run Whether User Is Logged On or Not”
  3. Check “Run With Highest Privileges”

Remove the Time Limit

  1. Click “Settings” in the tab bar
  2. Uncheck “Stop the Task if It Runs Longer Than”
  3. Click “OK”

Restart and use all your new free time to reduce suffering in the universe! :D

--

--

Mike P. Sinn

I like optimizing societal resource allocation in order to minimize suffering in the universe. If you do too, say hi at https://crowdsourcingcures.org