2

Postgres server on Debian should run on Windows 11 boot.

Debian was installed using

wsl --install Debian

Postgres in Debian using

sudo apt install postgresql-12

Created /etc/wsl.conf as described in this answer:

[boot]
command="service postgresql start"

Created task scheduler task which runs on startup. Tried to run commands from it

wsl

and

wsl -u root 

I tried to run VB script from here:

set object = createobject("wscript.shell") 
object.run "wsl.exe", 0

However WSL and Postgres and are not started if Windows is re-booted.

Solutions in this answer seem to be outdated since /etc/wsl.conf should used to run daemons in WSL.

If VBS script is run from command line, Postgres server is started.

How to run Debian/Postgres as service in Windows 11 boot? Should SrvAny, NSSM or Startup Scheduled Task used?

wsl --version returns

Verze WSL: 1.0.3.0
Verze jádra: 5.15.79.1
Verze WSLg: 1.0.47
Verze MSRDC: 1.2.3575
Verze Direct3D: 1.606.4
Verze DXCore: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
verze Windows: 10.0.22621.1105
12
  • You can’t run WSL as a service, all you can do is automate WSL instance being started (scheduled task), and configure Postgres within WSL to automatically be started. What step are you stuck on?
    – Ramhound
    Commented Nov 26, 2022 at 10:00
  • I'm unable to make WSL running on Windows reboot from Scheduled Tasks. It does not start and administrative events log does not contain any information. Task history shows that task exited with exit code 0. Logging to windows and running VBS script from command line works. I'm looking for solution to start WSL automatically if windows is rebooted after power break.
    – Andrus
    Commented Nov 26, 2022 at 11:18
  • @Andrus In my experience, WSL tends to only run once the user has logged in interactively to Windows. I've confirmed in the past that the wscript.shell workaround use to work, at least at one time, but it might have been fragile enough that it no longer does for one reason or another. I'll try to find a chance to re-test, but since it requires a lot of reboots, it's not my favorite test ;-). What version of WSL are you running? The newer application package version (1.0.0, etc.) may be even more difficult to run this way. Commented Nov 26, 2022 at 13:00
  • 1
    I updated question and added WSL version. I installed using WSL --install Debian. After installation it switches to Czech language in Windows. Dont know to to revert it back to English. I dont have any Chezh locales installed.
    – Andrus
    Commented Nov 26, 2022 at 13:28
  • @Andrus Well that's additionally weird! Might warrant a new question on that topic separately. I've installed the Debian WSL distro from the Store and never had that problem personally. But could be a bug in a recent Store package update. Commented Nov 26, 2022 at 16:17

2 Answers 2

2

It appears that Microsoft broke the capability of starting WSL as a service or a scheduled task when is was moved to their store because it will no longer run in session 0.

These conclusions can be inferred from the discussions with Microsoft's Windows Subsystem for Linux teams found on here on github.

Makes it less useful for sure. Only info from MS is they are working on it. Whether they are remains to be seen.

7
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 24, 2022 at 17:25
  • To be fair, it was never easy to run WSL at Windows boot. The wscript workaround was quite the hack. Not that I'm opposed to hacks -- A lot of my answers qualify, but I also wasn't surprised when it broke. I agree that it's a likely assumption that the Session 0 problem is also causing the wscript hack to break, but I haven't confirmed it myself. I just don't know for sure that Microsoft's solution for running SSH (the main Session 0 problem) will end up fixing this. Commented Dec 24, 2022 at 18:52
  • 1
    set object = createobject("wscript.shell") object.run "wsl.exe", 0 still works with last WSL and Windows 11
    – Andrus
    Commented Dec 25, 2022 at 18:53
  • @Andrus I'm confused - I thought your question above is about it not working any longer? Commented Jan 31, 2023 at 2:55
  • It starts working. Maybe Windows 11 newer update fixed this.
    – Andrus
    Commented Feb 1, 2023 at 6:33
2

You can create a batch-file like e.g. startWsl2Services.bat in the shell:startup folder (Win-R, type shell-startup):

@echo off
echo "Starting Ubuntu-22.04..."
wsl -d Ubuntu-22.04 -u root service dbus start
echo "Done"

or whichever distro (in your case Debian) and service you want to get started after user login

2
  • wsl exits after this command.
    – Andrus
    Commented Mar 11, 2023 at 17:51
  • No it remains running, incl. the started services (The powershell windows closes, but this does not stop WSL2)
    – daboss
    Commented Mar 12, 2023 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .