How to make WSL run services at startup

After searching this site and various Q, it is clear that services and systemd is not available for WSL. I need to run a program in WSL every time I start my PC so I read this page on how to use crontab: How to run Ubuntu service on Windows (at startup)? Super User but I got confused because the format does not tally with the format in crontab.

In addition, that particular question was specific for SSH servers which requires that the security aspect if considered and dealth with resulting in overcomplication of steps. Irrespective, the steps explored in that qusetion have been tried and they did not work. In addition, that question is highly specific to SSH servers whilst this question deals with a general enviromental requirement i. I need to know HOW to run services in WSL (which may include but is not limited to SSH serrvers)

In effect - A more simplified solution is required than How to run Ubuntu service on Windows (at startup)? provides.

However this is my cron:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=cd /usr/local/src/:cd xmr-stak-cpu:cd bin/
@reboot . sudo ./xmr-stak-cpu

I have also done this:

Run bash/cron loop on start

Create a file called linux.bat in shell:startup

Paste: C:\Windows\System32\bash.exe -c 'while [ true ]; do sudo /usr/sbin/cron -f; done'

It does not work.

How can I run a service in WSL? Or is there a way to use Windows?

Because in Windows I have tried the following: using https://github.com/Microsoft/WSL/issues/612

Run: When the computer starts, 
Action: Start a program, 
Program: c:\Windows\system32\bash.exe, 
Arguments: -c "sudo  /xmr-stak-cpu/bin/xmr-stak-cpu -D"
Start in:  /usr/local/src/

And as you guessed, it still does not work. Frankly I wish I could do this in WSL because it is my preferred way but I will take any way.

I have a workstation with 96GB RAM and as such I will prefer to use this as the dual Linux/Windows machine and not my puny laptop.

I have tasks on both Linux and Windows and really need/prefer the Linux solution provided by Windows.

I have reviewed the other question and there is a package called Mysys that seems to provide a solution however this departs from the integration provided by Microsoft which was a great way forward.


In WSL, the linux distributions run only after a first linux command is invoked. If you wanna run a linux deamon (a service) you must configure the server in linux and run any command in that linux distribution.

There are many pages and answers that show how to create a script to start a WSL linux when your computer starts.

  • There is a wsl-autostart VBS script that you can use. You can install the script and change the commands.txt with custom linux commands.
  • There is a step by step tutorial to start automatically an ssh server on WSL.
  • There are also options to create policies to run the program as an initial tasks (using the gpedit.msc command) or a scheduled task that run at startup (using the taskschd.msc).

NOTE: If your program must be executed with sudo, you must configure it to start the program without asking a password.

  • Run visudo in the linux and add a line at the end of the file: %sudo ALL=NOPASSWD: /full/path/to/program

My example with mysql service

  1. Create line in /etc/sudoers (at WSL to prevent asking password):

     %sudo   ALL=(ALL) NOPASSWD: /usr/sbin/service mysql start
    
  2. Create .bat file in Windows startup directory with this line (dir find here: Win+R and shell:startup):

     wsl sudo service mysql start
    

After restarting the service, it will start automatically.


Another option:

1) Create a startup file in Linux at /etc/init-wsl:

#!/bin/sh
echo booting
service ssh start
and make the script executable

chmod +x /etc/init-wsl

2) Schedule executing this file at windows boot or log on

test

Since wsl.exe is able to run commands inside the wsl distro, we simply schedule to run this file through the command wsl -u root /etc/init-wsl. If you have multiple distros, you might want to specify which one with a -d flag, for instance wsl -d Ubuntu-20.04 -u root /etc/init-wsl