3

I'm honestly not sure if this is an issue with WSL or I'm just doing something wrong for Ubuntu in general, but I cannot get the cron service to run at start on my WSL system.

It starts just fine with:

sudo service cron start

But it doesn't start at boot even after:

sudo update-rc.d cron defaults
sudo update-rc.d cron enable

Version:

$ uname -a
Linux PC-01 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic
7
  • What is the output of systemctl --failed? Mar 21, 2020 at 13:56
  • @PauloTomé: No systemd on wsl. Apparently it has it's own microsoft made init system.
    – jesse_b
    Mar 21, 2020 at 13:57
  • The problem is with WSL. You have to use Windows Task Scheduler, but you can still run a Linux task by scheduling C:\WSL\Ubuntu\ubuntu1804.exe with the argument run /path/to/linux/executable.
    – bitinerant
    Mar 21, 2020 at 13:58
  • 1
    Maybe superuser.com/questions/1343558/… could help. Mar 21, 2020 at 14:03
  • 1
    @PauloTomé: Thanks, using an rc file did cross my mind but just doesn't seem right. I guess running linux on windows isn't quite right either though.
    – jesse_b
    Mar 21, 2020 at 14:05

2 Answers 2

4

Old question that I just found when searching for a potential "duplicate" for another question that was just asked. Putting the answer here since this one is the first search result for "wsl start services".

There's a current answer and a future answer (based on Windows 10/11 Preview versions of WSL currently available).

The current answer is that WSL doesn't have the concept of "startup services". Microsoft's init process is neither a SysVinit nor a Systemd init like on "normal" Linux systems.

If you'd like to start the services automatically, there are currently two ways to do it, with a third coming in the next WSL release:


  • Option 1: Set up a Windows "Scheduled Task" to run on login (not boot):

    • The task can be a "Basic Task" to
    • "Start a Program"
    • The the "Program" is wsl.exe
    • And the "Arguments" should be -u root service cron start

    That will run WSL at login as the root user (needed to start services) and run the service cron start command. Of course, this can be modified to run any service that has an init.d script. Note that this does not (at least currently) work if you schedule the task to run at Windows Boot, since WSL seems to have a requirement that the user be logged in in order to keep the process running in the background.


  • Option 2: Modify your shell startup to check if the service is running, and start it if needed.

    In your startup (typically ~/.bashrc) add the following line:

    wsl.exe -u root sh -c "service cron status || service cron start"
    

    Under "normal" Linux, you'd need to visudo and give yourself permission to run the command without a password (or type the password each time you log in). Using wsl.exe -u root from within WSL allows you to bypass this.


  • Option 3: A new feature in Windows 11 is the ability to specify startup tasks for WSL using the /etc/wsl.conf file. If you have Windows 11, create that file with the following lines:

    [boot]
    command="service cron start"
    

    According to the Microsoft doc, this will run the command as root when the WSL instance starts.

    If you need to run multiple commands at WSL startup, separate them with semicolons in the same command line:

    [boot]
    command="service ssh start; service cron start"
    
1
  • 1
    Thanks yeah I ended up doing it just with a batch script that runs wsl and starts cron as a windows startup script but nice to know the functionality to do it in a more correct-ish manner is coming.
    – jesse_b
    Aug 19, 2021 at 18:33
0

This post helped my get what I was looking for however I needed to run multiple commands (start xrdp, tweak eth0 mtu, update resolv.conf) and running wsl.exe multiple times would instantiate multiple root logins causing some issues with file locking resolv.conf possibly because WSL does some setup at each login, not positive.

I am using wsl2 Kernel version: 5.10.102.1 Windows 10 with Ubuntu 20.04.5 LTS. My needs were

  1. Start xrdp (so I can connect to a gui session)
  2. Set eth0 mtu to 1350 (WSL had trouble with connectivity while on VPN due to mtu)
  3. Update resolv.conf with upstream resolvers (kept getting updated by WSL regardless of generateResolvConf = false in wsl.config)

So to expand on the answer from @NotTheDr01ds here is what I did.

  1. Put all commands in a new shell script .mystartup.sh in home directory
  2. Call .mystartup.sh as root from .profile

Here is what I added to .profile

# startup stuff
# run startup script as root
wsl.exe -u root /home/user/.mystartup.sh

And here is .mystartup.sh

# startup stuff
# service xrdp start
service xrdp status || service xrdp start

# setup lower mtu for vpn links
ifconfig eth0 mtu 1350
echo "eth0 mtu updated!"

# remove link to /run/resolvconf/resolv.conf & create new
rm -f /etc/resolv.conf
touch /etc/resolv.conf
chmod u+rwx,g+rwx,o+rx /etc/resolv.conf

# setup nameservers
echo "# this resolv.conf created using user .profile script" >> /etc/resolv.conf
echo "# wsl auto-generate must be disabled in /etc/wsl.config" >> /etc/resolv.conf
echo "nameserver 10.0.0.50" >> /etc/resolv.conf
echo "nameserver 10.0.0.51" >> /etc/resolv.conf
echo "resolv.conf modified with custom dns resolvers!"
echo

You must log in to answer this question.

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