Wait until the process finishes in terminal and quit terminal

Hi all

I have this macro I run in finder to launch a CLI bash command

this works well but im left with an open terminal window I don't need

is there a way to make keyboard maestro close the terminal window after the bash process is finished?

thx a lot


According to the screenshot, the action inside the loop is not a default KM action. This is a custom action written by a user, who happens to be me (“Execute a Script in Terminal”).

It would probably be possible to close the Terminal window after successful exit (not without complications though), but it is not the purpose of this action to do this.

The action is meant for things you explicitly like (or have) to run in a visible Terminal window (instead of the “background” shell).[1] So, auto-closing the Terminal window would not be in line with the purpose of the action.

If you want to run command-line programs silently in the background, I propose to use the regular “Execute a Shell Script” KM action. (Output and/or errors will still be reported, according to the action settings.)

I hope I interpreted your issue correctly.

– Tom

1: For example for graphics-like output, or colorized output that only works in the Terminal; or table output, certain interactive processes, etc. Or for things that require a password and you don’t want to pipe it through KM/AppleScript.


thx tom :smiley:

I actually do want the interaction of the terminal (and not to run it in the background) since there is sometimes a need to give input to the beets process running.

so are there any alternate options i can use? Perhaps just kill all terminal windows after X seconds?

thx again


You could add ‘exit 0’ to the end of the terminal commands, which should close the window.

beet import “$KMVAR_Path ; exit 0

Note that it will do so as soon as the command finishes, so if you need to see anything first, that might not be a good option, but I presume that you don’t need it since you’re referring to it as a window you don’t need.

If you only want to close the window IFF the beet command exits cleanly, use this instead:

beet import “$KMVAR_Path && exit 0

which tells exit to run only if beet also exits with an exit code = 0 aka “no errors”

Yep, this is another use-case for that action. I added it to the footnote in my reply above.

@tjluoma’s solution seems to be a good one, I would give it a try. Alternatively you could:

  • simply send a Quit / Force Quit to the Terminal after “X seconds”, if you are sure your processes have finished then;
  • via AppleScript: test if a Terminal window is busy; if not, close it. This might give false positives in certain cases.

Here an example how to close non-busy windows via AppleScript:

tell application "Terminal"
  set windowCount to (count of the windows)
  if windowCount is greater than 0 then
    set allWindows to windows
    repeat with w in allWindows
      if w is not busy then close w
    end repeat
  end if
end tell

thx guys!

@tjluoma, the commands you pasted seems cool but they don't actually close there window but rather leaves a messages saying Process completed. See image:

any clue how to make it actually close the terminal window?

thx again


Oh I forgot that Terminal does that by default. It’s one of the first things that I change:

Goto Terminal.app » Preferences » Profiles » Shell » When the shell exits (as shown here):

Choose “Close the window” or “Close if the shell exited cleanly.”

ahh I see, I think that helped!

thx so much