Run command on exit #11597

Closed
opened 2026-01-31 02:52:06 +00:00 by claunia · 2 comments
Owner

Originally created by @pinxau1000 on GitHub (Nov 26, 2020).

Is it possible to run a specific command when exiting the profile?

By "exiting a profile" I mean closing the profile tab or closing the windows terminal with that profile openned.

Detailed Description

We can set how the profile behaviours when exiting with the "closeOnExit" property, but I need to run extra commands to shut down my virtual machine (VM).
I’m starting a VM with vagrant and establishing a ssh connection to it by setting the “commandline” property:

"commandline": "cmd.exe /k \"vagrant up && vagrant ssh\"",

Is it possible to run custom commands when exiting the profile? The custom commands I want to run are exit and then vagrant halt. The 1st command will exit the ssh connection and the 2nd will shut down the VM.

The image below shows all the workflow needed.
CMDonExit

Thanks.
pinxau1000

Originally created by @pinxau1000 on GitHub (Nov 26, 2020). ### Is it possible to run a specific command when exiting the profile? ###### By "exiting a profile" I mean closing the profile tab or closing the windows terminal with that profile openned. #### Detailed Description We can set how the profile behaviours when exiting with the "closeOnExit" property, but I need to run extra commands to shut down my virtual machine (VM). I’m starting a VM with vagrant and establishing a ssh connection to it by setting the “commandline” property: ```json "commandline": "cmd.exe /k \"vagrant up && vagrant ssh\"", ``` **Is it possible to run custom commands when exiting the profile?** The custom commands I want to run are ```exit``` and then ```vagrant halt```. The 1st command will exit the ssh connection and the 2nd will shut down the VM. The image below shows all the workflow needed. ![CMDonExit](https://user-images.githubusercontent.com/31994143/100349164-ecae7200-2fdf-11eb-99cc-3cc5f96a41c1.png) Thanks. pinxau1000
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-31 02:52:06 +00:00
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Nov 27, 2020):

Perhaps this could be done by having the profile run a separate executable that would run the other vagrant commands, call SetConsoleCtrlHandler, wait for CTRL_CLOSE_EVENT, and then run the final command. However, I am not sure the handler will be called if Microsoft Store kills OpenConsole.exe to upgrade Windows Terminal.

@KalleOlaviNiemitalo commented on GitHub (Nov 27, 2020): Perhaps this could be done by having the profile run a separate executable that would run the other `vagrant` commands, call [SetConsoleCtrlHandler](https://docs.microsoft.com/windows/console/setconsolectrlhandler), wait for CTRL_CLOSE_EVENT, and then run the final command. However, I am not sure the handler will be called if Microsoft Store kills OpenConsole.exe to upgrade Windows Terminal.
Author
Owner

@DHowett commented on GitHub (Dec 3, 2020):

Yeah -- this is something that the Terminal isn't going to do for you. Sorry. It might be a good thing for a third party tool to do, or even an extension (in the future), but in general I'm not interested in making Terminal quickly yell a bunch of bytes into a connection that's being torn down so that it can try to pick up the pieces. 😄

What would be best is if vagrant ssh had a mode to teardown on exit. docker run does (--rm), afterall

@DHowett commented on GitHub (Dec 3, 2020): Yeah -- this is something that the Terminal isn't going to do for you. Sorry. It might be a good thing for a third party tool to do, or even an extension (in the future), but in general I'm not interested in making Terminal quickly yell a bunch of bytes into a connection that's being torn down so that it can try to pick up the pieces. :smile: What would be best is if `vagrant ssh` had a mode to teardown on exit. `docker run` does (`--rm`), afterall
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#11597