DEV Community

Abhishek Sisodiya
Abhishek Sisodiya

Posted on • Updated on

Start mongodb/mysql services automatically in wsl2

  • First open a script using nano in your WSL terminal by running this command:
sudo nano ~/.local/bin/start_script
Enter fullscreen mode Exit fullscreen mode
  • Copy the following command in that scipt:
service mysql start
service mongodb start
Enter fullscreen mode Exit fullscreen mode

commands inside script

  • Press ctrl+o and then ctrl+x to save and exit the script.
  • Go to Windows start menu and type Task Scheduler to open Task Scheduler.
  • Under the Actions tab on the right hand side, click Create Task... to open create task window
  • On this window give your task a specific name and make sure Run only when user is logged on is selected as shown below. Task Name
  • Then click on Triggers tab and create a New Trigger by selecting Begin the Task At log on from the dropdown menu and under settings any user and select Enabled under Advanced Settings. Trigger
  • Then go to Actions Tab and create a New Action as Start a Program and under Settings set Program/Script as C:\Windows\System32\bash.exe and in the Add arguments -c "sudo ~/.local/bin/" as shown below. Actions

That's all. The next time Windows starts, task will be triggered and it will run your custom script as root and start mongodb and mysql. You can also add any other services to this script.

Discussion (0)