4

I'd like to automate Ubuntu through a single-click batch file in Windows.

I can just start ubuntu, but if I add /k cd "path", it just doesn't do anything. It exits with no error.

I'd like to do an automation so that I can click a batch file in Windows and:

  • open Ubuntu in WSL
  • cd to path
  • activate an environment
  • cargo run
  • ... and then leave Ubuntu running in the same instance

... but WSL doesn't seem to behave as I'd expect. I don't understand.

This question is really close, but I have tried it and can't get it to work.

NotTheDr01ds
  • 22,082

1 Answers1

3

Essentially, it sounds like you want to script a series of commands in WSL/Ubuntu, but leave the shell open after they are done.

The key steps are:

  • Use the wsl command, not the ubuntu.exe command.
  • Start the shell in which you are going to run the commands with -lic, which will make it both a login shell (to parse your ~/.bash_profile) and an interactive shell (to parse your ~/.bashrc).
  • Separate the commands with semicolons in the wsl arguments.
  • To keep a shell running after, make the final command exec bash

For example:

wsl -e bash -lic "cd /home/myhome/src/project ; source /home/myhome/src/myproject/bin/activate ; cargo run ; exec bash"
NotTheDr01ds
  • 22,082