4

In WSL2, since there's no systemd, it's necessary to use service to monitor and deal with services. I can list all the services with: sudo service --status-all

My question is simple: How can I add a new service in this list so I can interact with it?

For example, i want to create a service greeter. I created the following file greeter.service:

[Unit]
Description=My Greeting Service

[Service] Type=oneshot ExecStart=~/repos/random-scripts/greeter.sh

Where greeter.sh is a simple greeting shell script. I guess there is a specific directory I should copy this greeter.service file, but I can't figure which one. Any help would be appreciated.

1 Answers1

5

The greeter.service file that you show is actually a Systemd unit file. However, as you said, there's no direct support for Systemd in Ubuntu on WSL, so that particular approach is not going to work.

You have a few options:

Write a SysVInit script for the service

The service command is used to interact with older-style SysVInit services. Creating a SysVInit service is typically done by writing a shell script that responds to the arguments:

  • start
  • stop
  • restart
  • status

If there's an example here on Ask Ubuntu, it's buried a ways down in the search results. By the time Ask Ubuntu was created, I believe Upstart had already replaced SysVInit as the default init method, and then Systemd replaced Upstart.

But I did find this example on Github (MIT license), which looks reasonably solid.

As you can see from that boilerplate, your script is responsible for keeping track of the PID so that it can check if it is running (for status or restart calls), know how to kill it (for stop), etc.

You would place this script in /etc/init.d, where the service command will be able to interact with it.

Via another process supervisor

But Systemd and other successors to SysVInit came along because it's entirely possible to handle the boilerplate in a more standardized way for most services.

The problem with Systemd (as it related to WSL) is that it couples its PID1 Init process with the process supervision. WSL has its own init system which runs as PID1, which means we can't use the Systemd process supervision either.

But many other process supervisors are able to be decoupled from PID1, meaning they'll run just fine under WSL.

One of those that I know is quite compatible with WSL/Ubuntu (and is installable through apt) is Supervisord.

For that, you would create something like /etc/supervisor/conf.d/greeter.conf with:

[program:greeter]
command=/home/youruser/repos/random-scripts/greeter.sh

Start supervisord and then interact with your service via:

supervisorctl start greeter
supervisorctl stop greeter
...

A number of actions are available.

If you are on Windows 11, you can start supervisord automatically by adding it to your /etc/wsl.conf as in this answer.

On other distributions, I've run dinit (Artix Linux) and openrc (Alpine), but for Ubuntu, I'd probably stick with Supervisord.

NotTheDr01ds
  • 17,888