Score:4

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

th flag

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 avatar
us flag
Does this answer your question? [WSL Ubuntu, is there an easy way to have bash start automatically on Windows startup?](https://askubuntu.com/questions/1177273/wsl-ubuntu-is-there-an-easy-way-to-have-bash-start-automatically-on-windows-sta)
muru avatar
us flag
(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 avatar
th flag
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 avatar
us flag
You'd add a NOPASSWD rule for just this command, so you won't be prompted for a password.
ironsand avatar
th flag
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 avatar
us flag
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.
Score:6
vn flag

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
Jeppe avatar
es flag
It seems to happen on all instances I create of Ubuntu 18.04. I haven't added scripts like that.
NotTheDr01ds avatar
vn flag
@Jeppe Recommend you open a new question, then, but ["Sleeping for 1 second to let systemd settle"](https://www.google.com/search?q=%22Sleeping%20for%201%20second%20to%20let%20systemd%20settle%22), as you can see in that search, is almost certainly coming from [DamionGans/ubuntu-wsl2-systemd-script](https://github.com/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!
mbomb007 avatar
so flag
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 avatar
es flag
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 avatar
vn flag
@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.
Artem P avatar
ao flag
While it works for postgres, it doesn't for apache2. I had to use `pgrep apache2 > /dev/null || wsl.exe -u root -- service apache2 start > /dev/null`
NotTheDr01ds avatar
vn flag
@ArtemP True - Different services will need different startup methods on different distributions. I'll edit the answer to make that more clear.
Score:0
cn flag

At least as of March 2023 WSL2 now supports systemd (link), also on Windows 10. Just put

[boot]
systemd=true

in your /etc/wsl.conf. After a reboot you can enable services via systemctl.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.