Automatically Starting a Service when a WSL instance is Launched

Kristopher Wayton 0 Reputation points
2023-01-23T15:49:08.65+00:00

I'm using something called "wsl-vpnkit" to allow wsl to have network access when our AnyConnect VPN is running.

It works perfectly, but I have to open a powershell window and type this every time I reboot:

C:\ wsl -d wsl-vpnkit service wsl-vpnkit start

Is there a way that can happen automatically, every time the computer boots? Or maybe when I log into windows?

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,705 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,351 Reputation points
    2023-01-24T17:44:55.8366667+00:00

    Hello Kristopher Wayton,

    I would suggest to create a schedule task through Task Scheduler to run on Start.

    1. At the search box, type: task scheduler
    2. Open Task scheduler
    3. From Action menu select Create Task.
    4. At General tab, type a name for the task. e.g. "WSL Start" and select Run with highest privileges.

    5a. At Triggers tab, click New.

    5b. Select to Begin the task: At log on and click OK.

    6a. At Actions tab, click New.

    6b. At New Action window, click Browse.

    6c. Choose the application (WSL) that you want to run at startup and click Open.

    1. Introduce the arcguments "-d wsl-vpnkit service wsl-vpnkit start" Click OK.

    This will run the command line every time that the user logs in the computer.

    --If the reply is helpful, please Upvote and Accept as answer--

    3 people found this answer helpful.

  2. Michael Taylor 54,401 Reputation points
    2023-01-23T16:12:01.2966667+00:00

    If you want to run a command whenever you boot or log into a computer then consider using Task Scheduler. In general, if you want to start a service on boot then change the service startup type to Automatic (or delayed). Then the service will start automatically.

    If you need to run a command that isn't a service then create a scheduled task that runs at the appropriate time (such as when you log in). To do that go to the Task Scheduler.

    1. Create a new task using the Create Task option. Note that I recommend you keep your personal tasks in a subfolder so you can easily find them.
    2. Give the task a name and then select who the task runs as. The options here are either a dedicated user account when the user logs in or a dedicated account when anyone is logged in. In your case it sounds like you want it at your log in only. Also set whether it requires highest privileges (aka admin) or not. This would be required if it needs to run processes as admin.
    3. On the Triggers tab create a new trigger that begins at log on. You can specify whether all users or only a specific user has the task run. In your case I believe you want to have it run only when you log in. Alternatively you can have the task run at startup (if it isn't a service). Note that you should not enable any auto stop options here if you need this to run for days. Also you may specify a delay if the task requires other things to be only and they may take a little bit.
    4. On the Actions tab add a new action to start the program. Note that the Program/Script is the path and name of the script/exe to run. Arguments for that script go in the arguments field later. I recommend you set the Start in field to the working directory that the program runs in. The task will be run by a service so the working directory is wrong for scripts if you don't set this.
    5. On the Settings tab you should probably disable the options to stop the task if it runs too long and allow it to be run on demand. You should probably also consider having it not run if it is already running but for scripts that isn't likely an issue.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.