Feature Request: option to relaunch a profile when it exits #6176

Closed
opened 2026-01-31 00:31:39 +00:00 by claunia · 13 comments
Owner

Originally created by @tatroc on GitHub (Jan 28, 2020).

If I have a ssh.exe session open to another machine using ssh.exe (Openssh) and I disconnect from the network the session hangs in the Windows Terminal. I must close the tab. Could we make the session resume by pressing enter? Or is there a better way to handle this?

{
"guid": "{5f77e6d6-8215-45d0-be70-a277c72accf6}",
"hidden": false,
"name": "user@host",
"commandline": "ssh.exe user@host",
"fontFace": "Consolas",
"fontSize": 12,
"icon": "%USERPROFILE%\AppData\Local\lxss\bash.ico"
},

Originally created by @tatroc on GitHub (Jan 28, 2020). If I have a ssh.exe session open to another machine using ssh.exe (Openssh) and I disconnect from the network the session hangs in the Windows Terminal. I must close the tab. Could we make the session resume by pressing enter? Or is there a better way to handle this? { "guid": "{5f77e6d6-8215-45d0-be70-a277c72accf6}", "hidden": false, "name": "user@host", "commandline": "ssh.exe user@host", "fontFace": "Consolas", "fontSize": 12, "icon": "%USERPROFILE%\\AppData\\Local\\lxss\\bash.ico" },
Author
Owner

@DHowett-MSFT commented on GitHub (Jan 31, 2020):

Thanks for the request! I've triaged it and converted it into a request to relaunch a profile when it exits.

@DHowett-MSFT commented on GitHub (Jan 31, 2020): Thanks for the request! I've triaged it and converted it into a request to relaunch a profile when it exits.
Author
Owner

@DHowett-MSFT commented on GitHub (Jan 31, 2020):

For now, you can relaunch a new copy of that profile with ctrl+shift+d (duplicateTab)

@DHowett-MSFT commented on GitHub (Jan 31, 2020): For now, you can relaunch a new copy of that profile with <kbd>ctrl+shift+d</kbd> (`duplicateTab`)
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Dec 29, 2020):

Would relaunching the profile preserve the screen buffer, window title, and WT_SESSION environment variable?

@KalleOlaviNiemitalo commented on GitHub (Dec 29, 2020): Would relaunching the profile preserve the screen buffer, window title, and WT_SESSION environment variable?
Author
Owner

@zadjii-msft commented on GitHub (Feb 9, 2021):

From #4772: It might be nice if this was an enum value for closeOnExit. Something like "reconnect" to automatically reconnect (or reconnect after prompting for enter or something) to re-spawn the connection.

There's also #3726 for "manually restart a connection", which is similar, but different.

@zadjii-msft commented on GitHub (Feb 9, 2021): From #4772: It might be nice if this was an enum value for `closeOnExit`. Something like `"reconnect"` to automatically reconnect (or reconnect after prompting for <kbd>enter</kbd> or something) to re-spawn the connection. There's also #3726 for "manually restart a connection", which is similar, but _different_.
Author
Owner

@thernstig commented on GitHub (May 11, 2021):

I wrote a duplicate for this (which I have now closed) that might contain some more context: https://github.com/microsoft/terminal/issues/10075 But in short, this happens all the time when in hibernation or on bad Wifi, and I want to preserve my screen buffer etc. to continue where I left of.

One solution would be to display some text at the bottom of the buffer similar to how like done here in the April 2021 release of VS Code: https://code.visualstudio.com/updates/v1_56#_untitled-editors-hint. So a Press Enter to relaunch shell might be an option.

But I also do like the closeOnExit: "reconnect" as an added thing for users.

@thernstig commented on GitHub (May 11, 2021): I wrote a duplicate for this (which I have now closed) that might contain some more context: https://github.com/microsoft/terminal/issues/10075 But in short, this happens all the time when in hibernation or on bad Wifi, and I want to preserve my screen buffer etc. to continue where I left of. One solution would be to display some text at the bottom of the buffer similar to how like done here in the April 2021 release of VS Code: https://code.visualstudio.com/updates/v1_56#_untitled-editors-hint. So a `Press `<kbd>Enter</kbd>` to relaunch shell` might be an option. But I also do like the `closeOnExit: "reconnect"` as an added thing for users.
Author
Owner

@masics commented on GitHub (Aug 12, 2021):

We need to reconnect in the same tab.

  • It will keep the order of the tabs
  • No need to close the original tab manually
  • It will keep the screen buffer
@masics commented on GitHub (Aug 12, 2021): We need to reconnect in the same tab. * It will keep the order of the tabs * No need to close the original tab manually * It will keep the screen buffer
Author
Owner

@SteffenBlake commented on GitHub (Aug 25, 2021):

I have created a very simple little powershell script one can utilize to sort of replicate this behavior.

Save the following script to: C:\Windows\System32\Keep-Alive.ps1

$command=$args[0]

while ($TRUE) {
    Invoke-Expression $command
    Start-Sleep -Seconds 1
    Write-Output "Scope ended. Press Enter to reload."
    Read-Host
}

Then you can put your SSH or whatnot call nested within this function call to loop it on exit.

For example I put the following as my command for my profile's Command Line field:

PowerShell Keep-Alive 'ssh rancher@Usagi -i \"C:\Users\MyName\Documents\SSH Keys\Usagi\Usagi.pem\"'

Then you get something like the following:

image

@SteffenBlake commented on GitHub (Aug 25, 2021): I have created a very simple little powershell script one can utilize to sort of replicate this behavior. Save the following script to: `C:\Windows\System32\Keep-Alive.ps1` ``` $command=$args[0] while ($TRUE) { Invoke-Expression $command Start-Sleep -Seconds 1 Write-Output "Scope ended. Press Enter to reload." Read-Host } ``` Then you can put your SSH or whatnot call nested within this function call to loop it on exit. For example I put the following as my command for my profile's Command Line field: `PowerShell Keep-Alive 'ssh rancher@Usagi -i \"C:\Users\MyName\Documents\SSH Keys\Usagi\Usagi.pem\"'` Then you get something like the following: ![image](https://user-images.githubusercontent.com/16726175/130714717-8463f1b5-003e-42fe-9326-a6d376ffbfa2.png)
Author
Owner

@swinder0161 commented on GitHub (Oct 22, 2021):

Highly needed feature for the reasons mentioned above.
Comparison: iterm2 for mac has this feature and would be great we have in windows terminal too.

WT is great but can be even better and the greatest terminal emulator out there.

@swinder0161 commented on GitHub (Oct 22, 2021): Highly needed feature for the reasons mentioned above. Comparison: iterm2 for mac has this feature and would be great we have in windows terminal too. WT is great but can be even better and the greatest terminal emulator out there.
Author
Owner

@sisrfeng commented on GitHub (Feb 16, 2022):

modified from above :

  1. create a file called C:\Windows\System32\leo_keep.ps1
$leo_command=$args[0]

while ($TRUE) {
    Invoke-Expression $leo_command
    Start-Sleep -Seconds 1
    Write-Output "leo: ssh disconnected,  Press Enter to reload."
    Read-Host
}
  1. use PowerShell leo_keep 'ssh -X you@yourip' instead of 'ssh -X you@yourip at windows terminal' s profile

Set-ExecutionPolicy -ExecutionPolicy unrestricted

image

I think it is not that unsafe to use unrestricted
image


About Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass:

image

@sisrfeng commented on GitHub (Feb 16, 2022): ## modified from above : 1. create a file called `C:\Windows\System32\leo_keep.ps1` ``` $leo_command=$args[0] while ($TRUE) { Invoke-Expression $leo_command Start-Sleep -Seconds 1 Write-Output "leo: ssh disconnected, Press Enter to reload." Read-Host } ``` 2. use `PowerShell leo_keep 'ssh -X you@yourip' ` instead of `'ssh -X you@yourip` at windows terminal' s profile --- ## `Set-ExecutionPolicy -ExecutionPolicy unrestricted` ![image](https://user-images.githubusercontent.com/53520949/154267287-6aae59f1-ec91-4882-9f34-724fd049fc6c.png) I think it is **not that unsafe** to use `unrestricted` ![image](https://user-images.githubusercontent.com/53520949/154268393-507178ad-2c09-403e-924c-0e60caaa89d3.png) --- About `Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`: ![image](https://user-images.githubusercontent.com/53520949/154262876-c1d844aa-7a7d-4d1f-afc3-3983669dd88a.png)
Author
Owner

@swinder0161 commented on GitHub (Apr 13, 2022):

In wt profile use: wt_ssh.bat 192.168.1.100

modify ssh %1 to you command. No need of powershell to execute it. No policy change needed.

n followed by enter will terminate and exit
any other key followed by enter or simply press enter to reconnect.

wt_ssh.bat

@echo off

:START
ssh %1

echo.

set /a reconnect=y
set /p reconnect="ssh connection terminated, do you want to reconnect(Y/n)?"
rem echo reconnect: %reconnect%

echo.

if %reconnect%==n (echo exit) else (goto START)
@swinder0161 commented on GitHub (Apr 13, 2022): In wt profile use: wt_ssh.bat 192.168.1.100 modify ssh %1 to you command. No need of powershell to execute it. No policy change needed. n followed by enter will terminate and exit any other key followed by enter or simply press enter to reconnect. `wt_ssh.bat` ``` @echo off :START ssh %1 echo. set /a reconnect=y set /p reconnect="ssh connection terminated, do you want to reconnect(Y/n)?" rem echo reconnect: %reconnect% echo. if %reconnect%==n (echo exit) else (goto START) ```
Author
Owner

@IBNobody commented on GitHub (Apr 28, 2022):

In wt profile use: wt_ssh.bat 192.168.1.100

modify ssh %1 to you command. No need of powershell to execute it. No policy change needed.

n followed by enter will terminate and exit any other key followed by enter or simply press enter to reconnect.

wt_ssh.bat

@echo off

:START
ssh %1

echo.

set /a reconnect=y
set /p reconnect="ssh connection terminated, do you want to reconnect(Y/n)?"
rem echo reconnect: %reconnect%

echo.

if %reconnect%==n (echo exit) else (goto START)

Change ssh %1 to ssh %*. Some of us need ssh with extra switches.

@IBNobody commented on GitHub (Apr 28, 2022): > In wt profile use: wt_ssh.bat 192.168.1.100 > > modify ssh %1 to you command. No need of powershell to execute it. No policy change needed. > > n followed by enter will terminate and exit any other key followed by enter or simply press enter to reconnect. > > `wt_ssh.bat` > > ``` > @echo off > > :START > ssh %1 > > echo. > > set /a reconnect=y > set /p reconnect="ssh connection terminated, do you want to reconnect(Y/n)?" > rem echo reconnect: %reconnect% > > echo. > > if %reconnect%==n (echo exit) else (goto START) > ``` Change `ssh %1` to `ssh %*`. Some of us need ssh with extra switches.
Author
Owner

@EBADBEEF commented on GitHub (Jun 28, 2022):

I would also like this feature but if anyone else stumbles here like I did here is my one-liner workaround.

Automatically restart ssh in a loop: cmd /C for /L %i in (0,0,1) do @ssh user@host
Add a sleep in the loop: cmd /C "for /L %i in (0,0,1) do @ssh user@host & timeout /T 2"
Add a command to ssh (for me, reattach tmux): cmd /C "for /L %i in (0,0,1) do @ssh user@host -t . ~/.bashrc; my-tmux-bash-function %COMPUTERNAME% & timeout /T 2"

I couldn't figure out how to use '&' in the ssh command because windows cmd/for will interpret that. There is probably a way to escape it.

@EBADBEEF commented on GitHub (Jun 28, 2022): I would also like this feature but if anyone else stumbles here like I did here is my one-liner workaround. Automatically restart ssh in a loop: `cmd /C for /L %i in (0,0,1) do @ssh user@host` Add a sleep in the loop: `cmd /C "for /L %i in (0,0,1) do @ssh user@host & timeout /T 2"` Add a command to ssh (for me, reattach tmux): `cmd /C "for /L %i in (0,0,1) do @ssh user@host -t . ~/.bashrc; my-tmux-bash-function %COMPUTERNAME% & timeout /T 2"` I couldn't figure out how to use '&' in the ssh command because windows cmd/for will interpret that. There is probably a way to escape it.
Author
Owner

@ghost commented on GitHub (Jan 24, 2023):

:tada:This issue was addressed in #14060, which has now been successfully released as Windows Terminal Preview v1.17.1023.🎉

Handy links:

@ghost commented on GitHub (Jan 24, 2023): :tada:This issue was addressed in #14060, which has now been successfully released as `Windows Terminal Preview v1.17.1023`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v1.17.1023) * [Store Download](https://www.microsoft.com/store/apps/9n8g5rfz9xk3?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#6176