Dynamic profiles Automatically run command on open #5366

Closed
opened 2026-01-31 00:11:34 +00:00 by claunia · 12 comments
Owner

Originally created by @krisavi on GitHub (Dec 3, 2019).

Description of the new feature/enhancement

When I create a WSL profile manually using WSL.exe command then there is a possibility to modify start command and add to it to make it to start for example ssh session to some specific location I frequently use. Of course it would mean of changing the start command instead of having another parameter that will be run upon start of a tab with specific profile.
That functionality is missing for dynamic profiles.

Proposed technical implementation details (optional)

Field in profile.json to be able to set up a command that will be run as soon as profile is started. I have no idea how that could be named, tried to think of many, but they do not seem to be catch or self-explanatory. Some examples I thought of are "autorunCommand", "cmdAtStart".
Of course to make it more usable as you cannot make multiple profiles for dynamic profiles, then #3818 should be fixed as well. Then you could have one WSL that is regular, other one to run "sudo su" as a first command to go to elevated WSL instance, and maybe one that will be used as ssh agent by running something like "ssh user@yourFrequentSite.com"

Same parameter and functionality could be useful for other profiles that are not dynamic ones. I have seen some examples of people having multiple PS configuration with (at least seemed to be) ming64 or sys2 options set for the terminal.

Originally created by @krisavi on GitHub (Dec 3, 2019). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> # Description of the new feature/enhancement When I create a WSL profile manually using WSL.exe command then there is a possibility to modify start command and add to it to make it to start for example ssh session to some specific location I frequently use. Of course it would mean of changing the start command instead of having another parameter that will be run upon start of a tab with specific profile. That functionality is missing for dynamic profiles. <!-- A clear and concise description of what the problem is that the new feature would solve. Describe why and how a user would use this new functionality (if applicable). --> # Proposed technical implementation details (optional) Field in profile.json to be able to set up a command that will be run as soon as profile is started. I have no idea how that could be named, tried to think of many, but they do not seem to be catch or self-explanatory. Some examples I thought of are "autorunCommand", "cmdAtStart". Of course to make it more usable as you cannot make multiple profiles for dynamic profiles, then #3818 should be fixed as well. Then you could have one WSL that is regular, other one to run "sudo su" as a first command to go to elevated WSL instance, and maybe one that will be used as ssh agent by running something like "ssh user@yourFrequentSite.com" Same parameter and functionality could be useful for other profiles that are not dynamic ones. I have seen some examples of people having multiple PS configuration with (at least seemed to be) ming64 or sys2 options set for the terminal. <!-- A clear and concise description of what you want to happen. -->
claunia added the Issue-QuestionArea-SettingsResolution-AnsweredProduct-Terminal labels 2026-01-31 00:11:34 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Dec 3, 2019):

You can actually already set a commandline for dynamic profiles. We don't put that value in the stub we generate for dynamic profiles to try and hide some implementation details, but you're 100% free to just set your own commandline and override it.

For example, you could just set the commandline to the following:

"commandline": "wsl.exe -d Ubuntu-18.04 -- ssh me@myserver.com"

to have the profile autorun ssh me@myserver.com" in the "Ubuntu-18.04" distro.

@zadjii-msft commented on GitHub (Dec 3, 2019): You can actually already set a `commandline` for dynamic profiles. We don't put that value in the stub we generate for dynamic profiles to try and hide some implementation details, but you're 100% free to just set your own `commandline` and override it. For example, you could just set the commandline to the following: ```json "commandline": "wsl.exe -d Ubuntu-18.04 -- ssh me@myserver.com" ``` to have the profile autorun `ssh me@myserver.com"` in the "Ubuntu-18.04" distro.
Author
Owner

@krisavi commented on GitHub (Dec 5, 2019):

That seems like a problem if the WSL name should change, then the -d parameter has to be updated. It seems like dynamic profiles main point was so they will be able to detect if they are set up on system or not and show according to that.
Now having to use the command line is just the same to defining whole command manually without using dynamic profile at all, making dynamic profile functionality itself quite pointless.

Way to sent command on opening session by having a field to trigger on tab open would let some specific terminals that do not let you send in command via command line parameter to still run something specific automatically.

@krisavi commented on GitHub (Dec 5, 2019): That seems like a problem if the WSL name should change, then the -d parameter has to be updated. It seems like dynamic profiles main point was so they will be able to detect if they are set up on system or not and show according to that. Now having to use the command line is just the same to defining whole command manually without using dynamic profile at all, making dynamic profile functionality itself quite pointless. Way to sent command on opening session by having a field to trigger on tab open would let some specific terminals that do not let you send in command via command line parameter to still run something specific automatically.
Author
Owner

@DHowett-MSFT commented on GitHub (Dec 5, 2019):

quite pointless

The dynamic profile would still appear/disappear based on the presence of the underlying distribution, as it doesn’t do the lookup based on the profile’s conmandline. Rather, the GUID that’s stamped into the profile is derived from the distribution and will not match if the distribution has its name changed or is removed.

@DHowett-MSFT commented on GitHub (Dec 5, 2019): > quite pointless The dynamic profile would still appear/disappear based on the presence of the underlying distribution, as it doesn’t do the lookup based on the profile’s `conmandline`. Rather, the GUID that’s stamped into the profile is derived from the distribution and will not match if the distribution has its name changed or is removed.
Author
Owner

@zadjii-msft commented on GitHub (Dec 5, 2019):

For the record, a WSL distro's name won't ever change. If it does change somehow, then we'll end up generating another profile for it, sine the name is the unique key here. Even if you did specify the whole commandline for a distro profile, then it still retains the functionality of hiding itself if it detects the distro is uninstalled.

edit: frick Dustin sniping me again 😝

@zadjii-msft commented on GitHub (Dec 5, 2019): For the record, a WSL distro's name won't ever change. If it does change somehow, then we'll end up generating another profile for it, sine the name is the unique key here. Even if you did specify the whole commandline for a distro profile, then it still retains the functionality of hiding itself if it detects the distro is uninstalled. edit: frick Dustin sniping me again 😝
Author
Owner

@Luis-Palacios commented on GitHub (Aug 20, 2020):

You can actually already set a commandline for dynamic profiles. We don't put that value in the stub we generate for dynamic profiles to try and hide some implementation details, but you're 100% free to just set your own commandline and override it.

For example, you could just set the commandline to the following:

"commandline": "wsl.exe -d Ubuntu-18.04 -- ssh me@myserver.com"

to have the profile autorun ssh me@myserver.com" in the "Ubuntu-18.04" distro.

@zadjii-msft I'm trying your suggestion for running a specific initial command on my ubuntu but is not working I'm doing

"commandline": "wsl.exe -d Ubuntu-20.04 -- nvm list"

but the result is:
image

If I simply remove the -- nvm list from the commandline
It works and I can use nvm list manually.
image

Am I doing something wrong? or is this solution no longer supported? My goal is to have multiple profiles for the same ubuntu image but being able to run startup commands on the profiles such as setting my specific node or python version via the startup command

@Luis-Palacios commented on GitHub (Aug 20, 2020): > You can actually already set a `commandline` for dynamic profiles. We don't put that value in the stub we generate for dynamic profiles to try and hide some implementation details, but you're 100% free to just set your own `commandline` and override it. > > For example, you could just set the commandline to the following: > > ```json > "commandline": "wsl.exe -d Ubuntu-18.04 -- ssh me@myserver.com" > ``` > > to have the profile autorun `ssh me@myserver.com"` in the "Ubuntu-18.04" distro. @zadjii-msft I'm trying your suggestion for running a specific initial command on my ubuntu but is not working I'm doing ```json "commandline": "wsl.exe -d Ubuntu-20.04 -- nvm list" ``` but the result is: ![image](https://user-images.githubusercontent.com/4844997/90804697-d05fb280-e2d7-11ea-85c0-841157a4de62.png) If I simply remove the `-- nvm list` from the commandline It works and I can use `nvm list` manually. ![image](https://user-images.githubusercontent.com/4844997/90804829-ff762400-e2d7-11ea-911f-73d2a9ce6486.png) Am I doing something wrong? or is this solution no longer supported? My goal is to have multiple profiles for the same ubuntu image but being able to run startup commands on the profiles such as setting my specific node or python version via the startup command
Author
Owner

@DHowett commented on GitHub (Aug 21, 2020):

@Luis-Palacios This is either because nvm is a shell function or is living somewhere specified by your personal startup scripts.

If it's a function, you need to get your shell engaged in the process of launching it.

The best way for you to make sure you can launch it is ...

  • Run type nvm
    • If it says "nvm is a function ...", then you'll need to make sure your shell is part of startup: wsl -- bash -c nvm might work.
    • If it says "nvm is /home/luis/path/to/nvm", you should just set your startup command to wsl -d xxxx -- /home/luis/path/to/nvm

This is because the argument that goes after wsl -- needs to be something that can be found directly without running your personal startup scripts.

@DHowett commented on GitHub (Aug 21, 2020): @Luis-Palacios This is either because nvm _is a shell function_ or is living somewhere specified by your personal startup scripts. If it's a function, you need to get your shell engaged in the process of launching it. The best way for you to make sure you can launch it is ... * Run `type nvm` * If it says "nvm is a function ...", then you'll need to make sure your shell is part of startup: `wsl -- bash -c nvm` _might_ work. * If it says "nvm is /home/luis/path/to/nvm", you should just set your startup command to `wsl -d xxxx -- /home/luis/path/to/nvm` This is because the argument that goes after `wsl --` needs to be _something that can be found directly without running your personal startup scripts._
Author
Owner

@Luis-Palacios commented on GitHub (Aug 21, 2020):

Thanks @DHowett nvm is indeed a shell also pyenv function, I have updated to

... -- bash -c path/to/pyenv versions and now kinda works I can see the command is executed but at the end of the output I see a [process exited with code 1] and I'm not able to use the terminal

image

@Luis-Palacios commented on GitHub (Aug 21, 2020): Thanks @DHowett nvm is indeed a shell also pyenv function, I have updated to ` ... -- bash -c path/to/pyenv versions` and now kinda works I can see the command is executed but at the end of the output I see a `[process exited with code 1]` and I'm not able to use the terminal ![image](https://user-images.githubusercontent.com/4844997/90838691-c1e2bc80-e312-11ea-8681-0b00f9249e0f.png)
Author
Owner

@DHowett commented on GitHub (Aug 21, 2020):

Since it has spaces in it, it might work better if you wrap it in quotes. bash -c "pyenv xyz". Since it goes into JSON, make sure you escape it properly (\")

@DHowett commented on GitHub (Aug 21, 2020): Since it has spaces in it, it might work better if you wrap it in quotes. `bash -c "pyenv xyz"`. Since it goes into JSON, make sure you escape it properly (`\"`)
Author
Owner

@Luis-Palacios commented on GitHub (Aug 21, 2020):

using quotes is indeed needed, but this makes another problem come up, it seems I need my scripts on my ~/.zshrc to be executed in order for nvm and pyenv to work correctly, @DHowett is there a way to make commandline execute a command once my startup scripts have been executed?

@Luis-Palacios commented on GitHub (Aug 21, 2020): using quotes is indeed needed, but this makes another problem come up, it seems I need my scripts on my `~/.zshrc` to be executed in order for nvm and pyenv to work correctly, @DHowett is there a way to make commandline execute a command once my startup scripts have been executed?
Author
Owner

@Luis-Palacios commented on GitHub (Aug 21, 2020):

I just tried

"commandline": "wsl.exe -d Ubuntu-20.04 -- bash -c 'source ~/.zshrc && pyenv activate granadatilesenv'"

but it runs into syntax errors I suppose due to scaping characters?

/home/luis/.zshrc: line 4: ${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh: bad substitution
/home/luis/.oh-my-zsh/tools/check_for_upgrade.sh: line 31: syntax error near unexpected token `)'
/home/luis/.oh-my-zsh/tools/check_for_upgrade.sh: line 31: `() {'
/home/luis/.oh-my-zsh/oh-my-zsh.sh: line 18: autoload: command not found
/home/luis/.oh-my-zsh/oh-my-zsh.sh: line 36: syntax error near unexpected token `('
/home/luis/.oh-my-zsh/oh-my-zsh.sh: line 36: `for plugin ($plugins); do'
/home/luis/.p10k.zsh: line 16: local: can only be used in a function
/home/luis/.p10k.zsh: line 20: builtin: setopt: not a shell builtin
/home/luis/.p10k.zsh: line 22: syntax error near unexpected token `)'
/home/luis/.p10k.zsh: line 22: `() {'
@Luis-Palacios commented on GitHub (Aug 21, 2020): I just tried ```json "commandline": "wsl.exe -d Ubuntu-20.04 -- bash -c 'source ~/.zshrc && pyenv activate granadatilesenv'" ``` but it runs into syntax errors I suppose due to scaping characters? ``` /home/luis/.zshrc: line 4: ${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh: bad substitution /home/luis/.oh-my-zsh/tools/check_for_upgrade.sh: line 31: syntax error near unexpected token `)' /home/luis/.oh-my-zsh/tools/check_for_upgrade.sh: line 31: `() {' /home/luis/.oh-my-zsh/oh-my-zsh.sh: line 18: autoload: command not found /home/luis/.oh-my-zsh/oh-my-zsh.sh: line 36: syntax error near unexpected token `(' /home/luis/.oh-my-zsh/oh-my-zsh.sh: line 36: `for plugin ($plugins); do' /home/luis/.p10k.zsh: line 16: local: can only be used in a function /home/luis/.p10k.zsh: line 20: builtin: setopt: not a shell builtin /home/luis/.p10k.zsh: line 22: syntax error near unexpected token `)' /home/luis/.p10k.zsh: line 22: `() {' ```
Author
Owner

@DHowett commented on GitHub (Aug 21, 2020):

The main issue there is that you're using bash to run zshrc. Try zsh -c 'source ~/.zshrc ...............'!

@DHowett commented on GitHub (Aug 21, 2020): The main issue there is that you're using bash to run `zshrc`. Try `zsh -c 'source ~/.zshrc ...............'`!
Author
Owner

@Luis-Palacios commented on GitHub (Aug 21, 2020):

@DHowett thanks for bearing with such a linux noob.

I have:

"commandline": "wsl.exe -d Ubuntu-20.04 -- zsh -c 'source ~/.zshrc && pyenv activate granadatilesenv'"

When I start the windows terminal with the profile it seems to launch the command correctly without any errors however the output is quickly cleared and it kinda seems the shell exits

Untitled_ Aug 20, 2020 7_48 PM

Expected:

image

Reading this stackoverflow question

There is a claim that "wsl.exe (which runs your default shell such as bash) does not currently support opening a shell with a command without exiting the shell right after the command is run."

Could this be what I'm facing? I tried the suggesting there about using powershell but it does not seem to work either

"commandline": "powershell.exe -NoExit -Command wsl.exe -d Ubuntu-20.04 -- zsh -c 'source ~/.zshrc && pyenv activate granadatilesenv'"

Output:

image

Any other idea?

@Luis-Palacios commented on GitHub (Aug 21, 2020): @DHowett thanks for bearing with such a linux noob. I have: ```json "commandline": "wsl.exe -d Ubuntu-20.04 -- zsh -c 'source ~/.zshrc && pyenv activate granadatilesenv'" ``` When I start the windows terminal with the profile it seems to launch the command correctly without any errors however the output is quickly cleared and it kinda seems the shell exits ![Untitled_ Aug 20, 2020 7_48 PM](https://user-images.githubusercontent.com/4844997/90843340-a087cd80-e31e-11ea-8fe5-2935f185a4a4.gif) Expected: ![image](https://user-images.githubusercontent.com/4844997/90843481-f3fa1b80-e31e-11ea-83b4-7f30dad3f3be.png) Reading this [stackoverflow question](https://stackoverflow.com/questions/62468029/windows-terminal-open-multiple-panes-and-execute-specified-command) There is a claim that "wsl.exe (which runs your default shell such as bash) does not currently support opening a shell with a command without exiting the shell right after the command is run." Could this be what I'm facing? I tried the suggesting there about using powershell but it does not seem to work either ```json "commandline": "powershell.exe -NoExit -Command wsl.exe -d Ubuntu-20.04 -- zsh -c 'source ~/.zshrc && pyenv activate granadatilesenv'" ``` Output: ![image](https://user-images.githubusercontent.com/4844997/90843440-de84f180-e31e-11ea-8c87-4d40e9437bea.png) Any other idea?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#5366