Search code examples
bashwindows-subsystem-for-linux

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


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?


Solution

  • 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 Core:

    (Get-ComputerInfo).WindowsUBR
    

    Or from older versions of PowerShell:

    (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').UBR
    

    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).