How to run a command on WSL startup using WSL config files?
Asked Answered
V

1

1

I added these lines in my WSL settings files

C:\Users\reynadan\.wslconfig:

[boot]
command=bash /home/reynadan/scripts/startup.sh

/etc/wsl.conf:

[boot]
command=bash /home/reynadan/scripts/startup.sh

/home/reynadan/scripts/startup.sh

    #!/bin/bash

    # Run wsl-vpnkit if not already connected or running
    currentlyRunningWsl=$(wsl.exe -l --running | iconv -f UTF16 -t UTF8 | grep wsl-vpnkit | wc -l)
    
    if [[ $currentlyRunningWsl -eq 0 ]]; then
        wsl.exe -d wsl-vpnkit service wsl-vpnkit start
    fi
    
    # Start Docker daemon automatically when logging in if not running.
    RUNNING=`ps aux | grep dockerd | grep -v grep`
    if [ -z "$RUNNING" ]; then
            sudo dockerd > /dev/null 2>&1 &
            disown
    fi
    NOW=$(date)
    echo WSL booted at $(/bin/date +'%Y-%m-%d %H:%M:%S') >> /home/reynadan/wslBootHistory.txt
    echo 'startup lanched'

I closed with wsl --shutdown and waited more than 8 seconds before running it again, but /home/reynadan/wslBootHistory.txt is still empty and docker is not running.

How do I make sure WSL runs my script on startup?

Veronica answered 2/12, 2022 at 10:38 Comment(3)
Just confirming you are using Windows 11 or Server 2022? As the [boot] is only available on those OS.Michaels
Well Occam's razor is still cutting sharp, I'm on Windows 10, please add an answer so I can mark as solvedVeronica
@Veronica Just noticed the "two config files" -- I would have addressed this in my answer, but the only one that allows the [boot].command is the /etc/wsl.conf, since you can have a separate startup process for multiple distributions. %userprofile%\.wslconfig, on the other hand, is shared among all WSL distributions -- [boot].command doesn't apply there.Warfare
W
5

As noted in the comments, you are on Windows 10. However, the information in the comments is a bit outdated -- The boot.command feature does now work on Windows 10, but you need the absolute latest release (including optional updates) of both Windows 10 and WSL installed.

First, confirm that your system is running the November "Cumulative Update Preview". If you are, then your UBR (update-build-revision) will be at 2311 or higher. From PowerShell:

(Get-ComputerInfo).WindowsUBR

If it is lower than 2311, then:

  • First, make sure your system is otherwise completely up-to-date.
  • Go to Settings -> Check for Updates and press the Check for Updates button.
  • If you are fully updated on Windows 10 (but still running less than UBR 2311) you should see "2022-11 Cumulative Update Preview for Windows 10 Version 22H2 for x64-based Systems (KB5020030)" available as an optional update. Install it and reboot when prompted.

With that in place, you should now be able to update to the Store version of WSL with a simple:

wsl --update
wsl --version

After the update, you should be at:

WSL version: 1.0.0.0

... or later.

A reboot at this point is recommended for all features to work properly, but not strictly required.

At this point, you should have access to the /etc/wsl.conf's [boot].command feature.

If for some reason it's still not working, then I would recommend removing the script from the equation to troubleshoot. Try something like command=service cron start and see if the Cron service starts when you restart WSL.

Note that this new update also brings a number of other new WSL2 features to Windows 10 users, including:

  • Systemd support
  • WSLg: The ability to run Linux GUI applications in WSL2
  • The --mount argument for adding additional Windows's drives and partitions (including those with other filesystems or even raw partitions).
Warfare answered 2/12, 2022 at 12:20 Comment(6)
Windows is up to date ( last update : KB5020613) but I installed WSL with an in box version, wsl --update doesn't exist in this one. I'll upgrade it then thank you for your time :)Veronica
@Veronica Cool - Let me know how it goes. I really thought that wsl --update now takes the in-box version up to the Store version if you are on 2311, but I might be wrong. But yes, falling back to using the Store install of WSL should work as well. If it doesn't just re-confirm that Optional Update -- KB5020613 seems to just be a .NET update.Warfare
It works well using the store version of wsl 2, thanks again !Veronica
I think that the deployment of WSL update via WU is slow and hasn't made it to everyone. I also had to install the store version in order to try out the new features.Chicle
@JulianKnight Well, to be clear, Windows update will never update WSL in the future. New versions are only released via the Store app (or direct download of the package). The wsl --update will now (at least on my Windows 10 22H2 system) update WSL * to the store version*.Warfare
Thanks @NotTheDr01ds, I think I caught it as the update was in-progress as installation was rather odd. the --update wasn't doing anything on my system and I had to manually install the store version which wasn't what the Microsoft instructions said in their updated docs.Chicle

© 2022 - 2024 — McMap. All rights reserved.