Start WSL2 services on Windows startup

By Dillon Smart · · · 0 Comments

Start Windows Subsystem for Linux services

In some circumstances, you may want to automatically start your WSL2 (Windows Subsystem for Linux) services on Windows startup such as Apache2, Nginx, and MySQL. Follow along below to learn how to start your services when you boot your machine.

Create a startup script

The first step is to create a startup script. This is the script that will be executed when you turn your machine on.

From the terminal, create and open a new shell script file:

nano /etc/init.sh

You may require elevated privileges, so use sudo if needed.

Within the new file, copy the following commands. You can add more commands for other services.

#!/bin/sh
service apache2 start
service mysql start

WSL2 start services on Windows Startup
After creating and saving the script you will need to make it executable:

chmod +x /etc/init.sh

Create a new Windows Task

From the start menu in Windows, search and open Task Scheduler.

In the right-hand panel, select Task Scheduler Library.

In the left-hand panel under Actions, select “Create Task…”. This will open a new window with the “General” tab selected.

Enter a name and description for your task.

Under security options within the “General” tab, select “Run whether user is logged on or not” and “Run with highest privileges”.

Create a new task scheduler task in Windows for WSL Windows Subsystem for Linux

Next, select the “Actions” tab > “New…”. This will open another window.

Enter the following in the Program/script input field.

wsl -d Ubuntu-18.04 -u root /etc/init.sh

Ensure you replace the Ubuntu version with the correct Ubuntu version.

Add an action to call WSL script in Windows Task

Select OK.

Finally, select the Conditions tab. From here you can configure the criteria that best suits you.

Select OK.

Conclusion

You have successfully created a new task to start WSL2 services on Windows startup. To make sure things are working correctly, reboot your machine.

For help setting up WSL for web developers, check out The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

UbuntuWindowsWindows Subsystem for Linux

0 Comment

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

Updated 16th August 2022

Windows is now good for development! Yes! All thanks to Windows Subsystem for Linux (WSL). No more WAMP or Xampp, WSL can do everything you need. In this ultimate guide to setup Windows Subsystem for Linux with Ubuntu, Apache2, MySQL, and PHP we will cover: What is Windows Subsystem for Linux? Setting up Windows Subsystem

What is a Cron Job and how to write to the Crontab

Updated 10th December 2023

The need to schedule tasks on a machine is common for any Software Developer, System Admin or IT professional, so it’s important to learn what a cron job is and how to write to the crontab. What is CRON? Cron is a daemon built-in to most Unix-like operating systems such as: Cron reads the crontab (cron tables)

WSL vs Dual Boot speed test

Updated 16th August 2022

In this experiment, I will be using WSL2 running Ubuntu 20.04 and Ubuntu 20.04 dual-booted on the same Dell XPS. Is there a noticeable difference? Follow along for the results of the WSL vs Dual Boot speed test. If you are like me and use different development environments a lot, you may find the results