How to start a specific service when Ubuntu is started on WSL2

6,037

With the recent release of Windows 11, there are two preferred ways to do this.

Windows 11

You can now execute an arbitrary command line when starting an instance by creating/editing /etc/wsl.conf (via sudo) with the following:

[boot]
command="service postgresql start"

This command runs as root and generates no output. If you need to run multiple commands, they should be semicolon separated (or something like &&) inside the command= string.

Windows 10

On WSL with Windows 10, there's still an easier way, IMHO, than putting a sudo command in your startup and worrying about sudoers.

sudoers is certainly the canonical (no pun intended, just a happy accident) way to do it on Ubuntu, but on WSL it's just easier to use the following syntax in your ~/.bashrc:

 wsl.exe -u root service postgresql status || wsl.exe -u root service postgresql start

wsl.exe -u root doesn't require a password. From PowerShell and CMD, it can be called without the exe, but from within WSL it does require the extension.

Note, per @mbomb007's comment, this will generate one or two messages every time you start. To suppress this, use:

wsl.exe -u root service postgresql status > /dev/null || wsl.exe -u root service postgresql start > /dev/null
Share:
6,037

Related videos on Youtube

ironsand
Author by

ironsand

Updated on September 18, 2022

Comments

  • ironsand
    ironsand over 1 year

    I need to run sudo service postgresql start every time when I start Ubuntu on WSL2.

    How can I make the service start automatically when I start Ubuntu?

    WSL doesn't use systemd, so sudo systemctl enable postgresql doesn't work.

    $ sudo systemctl status postgresql
    System has not been booted with systemd as init system (PID 1). 
    Can't operate. Failed to connect to bus: Host is down
    

    Is there a standard way to start a service at startup?

    Edit

    I want to start the service on Ubuntu startup, not on Windows startup.

    • muru
      muru almost 3 years
    • muru
      muru almost 3 years
      (If you didn't mean start on boot, but only when you run WSL, then just putting sudo service postgresql start in your .bashrc should work fine.)
    • ironsand
      ironsand almost 3 years
      I just want to start a service when WSL is launched. And I'd avoid to sudo command in .bashrc. In that way I always need to type my password when I open new tabs.
    • muru
      muru almost 3 years
      You'd add a NOPASSWD rule for just this command, so you won't be prompted for a password.
    • ironsand
      ironsand almost 3 years
      I added ironsand ALL=NOPASSWD:/usr/sbin/service postgresql to /etc/sudoer and restart wsl, but I still need to enter password every time. Maybe this is another question, should I open a new question?
    • muru
      muru almost 3 years
      If you use an argument to the command, then it must match the command line exactly (sudo service postgresql != sudo service postgresql start. Try adding the start to the sudoers line as well. You shouldn't have to restart WSL, changes to sudoers take effect immediately.
  • mbomb007
    mbomb007 over 2 years
    To avoid seeing output every time you open WSL, use wsl.exe -u root service postgresql status > /dev/null || wsl.exe -u root service postgresql start > /dev/null instead.
  • Jeppe
    Jeppe over 2 years
    Windows 11 solution doesn't work for me.. service is still inactive. I can tell it loads slower before it shows Sleeping for 1 second to let systemd settle though.
  • NotTheDr01ds
    NotTheDr01ds over 2 years
    @Jeppe It sounds like you are using a Systemd script -- Perhaps WSL2Hacks or Genie? That's going to change a lot of behavior on your system. If you have Systemd "working", though, just use the normal systemctl methods to autostart -- Something like sudo systemctl enable <servicename>. But strongly recommend you understand the possible caveats you can run into with Systemd under WSL2. This is just one of many things that will be different.
  • Jeppe
    Jeppe about 2 years
    It seems to happen on all instances I create of Ubuntu 18.04. I haven't added scripts like that.
  • NotTheDr01ds
    NotTheDr01ds about 2 years
    @Jeppe Recommend you open a new question, then, but "Sleeping for 1 second to let systemd settle", as you can see in that search, is almost certainly coming from DamionGans/ubuntu-wsl2-systemd-script. If you aren't installing it yourself, then please include information in your question about how you installed Ubuntu 18.04 on WSL, because the script definitely seems to be getting installed on your system somehow. Thanks!