1

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
NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
Andrus
  • 149
  • 2
  • 9
  • 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 Nov 26 '22 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 Nov 26 '22 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. – NotTheDr01ds Nov 26 '22 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 Nov 26 '22 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. – NotTheDr01ds Nov 26 '22 at 16:17
  • @Andrus And I have a sneaking suspicion that the 1.0.0 version isn't going to work since it is now installed as a UWP app rather than a Windows feature. The switch to the app also ["broke" SSH support](https://superuser.com/a/1714740/1210833), and the root issue may be the same here. Again, if I get a chance, I'll try to give it a shot, but we may be out-of-luck here. – NotTheDr01ds Nov 26 '22 at 16:20
  • So only possibility is to run VBS script in shell:startup folder? This requires logging on. Logging requires to store unencrypted password in windows registry. How to log on automatically without storing unencrypted password in computer? shutdown command has such option. – Andrus Nov 26 '22 at 17:18
  • @Andrus - You can’t automatically login into a specific user without storing the password. Windows doesn’t switch to localities that require a language pack that isn’t installed. – Ramhound Nov 26 '22 at 18:36
  • Shutdown ˇ/gˇ switch does this. https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown Maybe password can stored in hashed form. – Andrus Nov 27 '22 at 08:34
  • Does it have to use WSL? Have you considered running Debian in a Hyper-V virtual machine on the Windows 11 machine? – user2871239 Nov 27 '22 at 20:04
  • WSL is not strict requirement. However WSL is like regular windows application, works nicely in windows. All windows drives are mounted automatically and are read/write. It alows to use windows commands from linux command line and linux command from windows command line. – Andrus Nov 28 '22 at 22:11

2 Answers2

1

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.

Geeky51
  • 91
  • 5
  • 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](/help/how-to-answer). – Community Dec 24 '22 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. – NotTheDr01ds Dec 24 '22 at 18:52
  • 1
    `set object = createobject("wscript.shell") object.run "wsl.exe", 0` still works with last WSL and Windows 11 – Andrus Dec 25 '22 at 18:53
  • @Andrus I'm confused - I thought your question above is about it *not working* any longer? – NotTheDr01ds Jan 31 '23 at 02:55
  • It starts working. Maybe Windows 11 newer update fixed this. – Andrus Feb 01 '23 at 06:33
  • @Andrus. Can you confirm that you installed WSL from the windows store? I am not able to make that version start at boot, although it easily starts at the user login ([see my superuser question] (https://superuser.com/questions/1765824/can-one-start-wsl-at-boot-windows-11-pro-as-of-jan-30-2023). Did you do anything else unusual that may explain your success? Can anyone else confirm success? – Geeky51 Feb 08 '23 at 17:06
  • I installed it using windows update. Currently version numbers are bigger, I updated question with them. Unusual thing was that WSL swithed to Czech language suddenly. I dont have any relation to this language and its locales are not installed. – Andrus Feb 08 '23 at 21:13
0

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

daboss
  • 3
  • 3