Skip to content

Instantly share code, notes, and snippets.

@cromat
Last active January 8, 2024 21:32
Show Gist options
  • Save cromat/a065136db5bdc40f21dc139567ad0340 to your computer and use it in GitHub Desktop.
Save cromat/a065136db5bdc40f21dc139567ad0340 to your computer and use it in GitHub Desktop.
Run redis inside WSL as background service on Windows startup

Step by step guide to run redis-server inside WSL(Windows Subsystem for Linux) on Windows

I have tried to setup redis as starting background task with wsl-autostart, Task Scheduler and with lot vbs scripts including one described here but none of them seemed to work.

In the end I have manually created a simple one that does the job. This script basically starts a hidden Ubuntu Window and starts redis-server inside it.

  1. Install WSL (this is tested with Ubuntu 18.04 version)

  2. Install redis-server inside WSL sudo apt install redis-server

  3. Add sudo permission to your user to execute service command without password Open sudoers file sudo visudo and add to end: your_username ALL=NOPASSWD:/usr/sbin/service redis-server or if you want to disable sudo passwords generally add this to the end: your_username ALL=(ALL:ALL) NOPASSWD:ALL

  4. Create vbs file e.g start-redis.vbs inside startup folder (Open Run and enter shell:startup)

  5. In vbs file insert following:

Set oShell = CreateObject("WScript.Shell")
oShell.Run "wsl", 0
oShell.Run "bash -c ""sudo service redis-server start --daemonize yes"""
  1. That's it. You can try it by running vbs script and then run htop inside WSL terminal. You should see that redis is running. You can also set this up to work with Windows Task Scheduler.
@opusmagna
Copy link

opusmagna commented Sep 12, 2020

I have created a batch script for this purpose, saved it and added it as a start-up program on my machine.

Please note that step 3 mentioned is still necessary.

@ECHO OFF
cmd /c wsl --exec sudo service redis-server start && Exit /B 5

@bhavin-matsol
Copy link

On ubuntu, In order to make this work, I needed to modify step 3 to add * wildcard at the end.
e.g.
your_username ALL=NOPASSWD:/usr/sbin/service redis-server *

@ThukuWakogi
Copy link

On ubuntu, In order to make this work, I needed to modify step 3 to add * wildcard at the end. e.g. your_username ALL=NOPASSWD:/usr/sbin/service redis-server *

This worked for me. Thank you. 🙏🙏

@Piggy69
Copy link

Piggy69 commented Oct 20, 2022

I have created a batch script for this purpose, saved it and added it as a start-up program on my machine.

Please note that step 3 mentioned is still necessary.

@ECHO OFF
cmd /c wsl --exec sudo service redis-server start && Exit /B 5

On ubuntu, In order to make this work, I needed to modify step 3 to add * wildcard at the end. e.g. your_username ALL=NOPASSWD:/usr/sbin/service redis-server *

Thank you!!! Worked perfectly.

@slwhitman
Copy link

slwhitman commented Sep 16, 2023

This command works great, but I am having issues telling Windows to keep WSL Ubuntu running.

Windows seems to kill the WSL Ubuntu process and takes the Redis daemon down with it.

See https://learn.microsoft.com/en-us/answers/questions/1292052/how-to-prevent-wsl2-from-automatically-stopping?comment=question#newest-question-comment

I found this microsoft/WSL#10138

which indicates wsl --exec dbus-launch true will keep the wsl process alive.

Thus far it seems to be working, so the fully modified command that I am using in VS Code tasks.json is:

"command": "cmd /c \"wsl --exec dbus-launch true && wsl --exec sudo service redis-server start --daemonize yes && wsl --exec redis-cli PING && Exit /B 5\"",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment