AKRA
AKRA

Reputation: 288

Automating the initialisation of multiple wsl instances

I would like to make a shell script (or something similar) which fires up 6 separate wsl terminals, and then executes a specific program on each one. Each program is a server, so it cannot be all done from the same terminal, since they are non-terminating programs.

Is it possible?

Any help would be much appreciated!

K

EDIT: so now I am seeing that a file newWsl.ps1 in powershell:

cmd.exe /c start wsl.exe --cd "~" 

Then running:

powershell -ExecutionPolicy ByPass -File .\newWsl.ps1

Is a start, however, there are very sparse docs now, it seems.

Ideally, something like:

cmd.exe /c start wsl.exe --cd "~" -run "script.sh" 

Is what I need (where script.sh lives in the wsl home directory)!

Upvotes: 1

Views: 1128

Answers (1)

NotTheDr01ds
NotTheDr01ds

Reputation: 20648

There are a few ways to do this. You don't mention your distribution, so I'm going to assume Ubuntu since it is the default on WSL:

  • Option 1: Supervisord (or equivalent)

    Normally, if you were starting six different servers in Linux, you'd want to do that through Systemd. WSL doesn't easily support Systemd (due to Systemd's insistence on running as PID1). However, we can easily use another process supervisor that doesn't require PID1. supervisord is one I've used before on Ubuntu for this.

    sudo apt install supervisor supervisor-doc
    

    Create a file in /etc/supervisor/conf.d/ for each of your servers. For instance:

    /etc/supervisor/conf.d/:

    [program:program1]
    command=/home/youruser/program1.sh
    autostart=true
    redirect_stderr=true
    stdout_logfile=/home/youruser/program1.log
    directory=/home/youruser
    user=youruser
    

    Create one conf for each of your servers.

    Then, starting sudo supervisord will start all of your servers. To do this when starting WSL:

    wsl -u root supervisord -c /etc/supervisor/supervisord.conf
    

    You can also then open another WSL instance and see them running with:

    ps -efH
    sudo supervisorctl status
    

    You can also see the captured output in each log file.

    See man supervisorctl and man supervisor for more information on configuring and controlling the servers.


  • Option 2: Tmux

    If you need all of the output from the servers in the foreground, I'd recommend running the servers through Tmux, which is installed in Ubuntu on WSL by default. Start multiple Tmux windows, each with a different application, via:

    wsl -e tmux new-session ~/program1.sh `; new-window ~/program2.sh `; new-window ~/program3.sh # and so on
    

    Then use Ctrl+B 1 (or 2, 3, etc.) to switch between them. Personally, I always switch my prefix to Ctrl+A for Tmux. That's the screen (its predecessor) default, and it's much less of a finger-twister to reach.


  • Option 3: PowerShell Start-Process

    Finally, what you probably think you are looking for (from the way your question is phrased) is Start-Process in PowerShell. It's default behavior is to create a new process for each application.

    Start-Process wsl -ArgumentList "~ -e ./program1.sh"
    Start-Process wsl -ArgumentList "~ -e ./program2.sh"
    Start-Process wsl -ArgumentList "~ -e ./program3.sh"
    

    And perhaps it is. While I'd prefer either the Supervisor or Tmux approaches, you may have a preference for multiple WSL terminals for your use-case.

Upvotes: 2

Related Questions