How do I get a Mac ".command" file to automatically quit after running a shell script?
Asked Answered
M

8

28

In my shell script, my last lines are:

...
echo "$l" done
done

exit

I have Terminal preference set to "When the shell exits: Close the window". In all other cases, when I type "exit" or "logout", in Terminal, the window closes, but for this ".command" file (I can double-click on my shell script file, and the script runs), instead of closing the window, while the file's code says "exit", what shows on the screen is:

...
$l done
logout

[Process completed]

...and the window remains open. Does anyone know how to get a shell script to run, and then just automatically quit the Terminal window on completion?

Thanks!

Moria answered 2/4, 2010 at 9:33 Comment(2)
Anyone at least have any clues on where I could search for this?Moria
same question here: #8799141Monad
M
28

I was finally able to track down an answer to this. Similar to cobbal's answer, it invokes AppleScript, but since it's the only window that I'd have open, and I want to run my script as a quick open-and-close operation, this more brutish approach, works great for me.

Within the ".command" script itself, "...add this line to your script at the end"

osascript -e 'tell application "Terminal" to quit' &
exit

SOURCE: http://forums.macosxhints.com/archive/index.php/t-2538.html

Moria answered 5/5, 2010 at 18:18 Comment(2)
Looks like the Terminal window prompts to confirm (since I don't want to have the Terminal preferences updated globally.)Slype
But this would close all the terminal windows even they were opened for other purposes.Heid
G
23

This worked perfectly for me.. it just closes that execution window leaving other terminal windows open

Just open Terminal and go to Terminal > Preferences > Settings > Shell: > When the shell exits: -> Close if the shell exited cleanly

Then just add exit; at the end of your file.

Giesser answered 21/2, 2013 at 23:1 Comment(2)
The accepted answer was closing the process (sourcetree in my case) started by the script. This answer just closed the terminal just as I expected.Gastrocnemius
For later Mac, Settings > Profiles tab > in right pane, “Shell” tab and then choose under the “When the shell exits”: Close if the shell exited cleanlyParvenu
F
5

Use the 'Terminal > Preferences > Settings > Shell: > When the shell exits: -> Close if the shell exited cleanly' option mentioned above, but put

exit 0

as the last line of your command file. That ensures the script really does 'exit cleanly' - otherwise if the previous command doesn't return success then the window won't close.

Footsore answered 16/6, 2015 at 18:39 Comment(0)
S
1

Short of having to use the AppleScript solutions above, this is the only shell script solution that worked (exit didn't), even if abruptly, for me (tested in OS X 10.9):

...
echo "$l" done
done

killall Terminal

Of course this will kill all running Terminal instances, so if you were working on a Terminal window before launching the script, it will be terminated as well. Luckily, relaunching Terminal gets you to a "Restored" state but, nevertheless, this must be considered only for edge cases and not as a clean solution.

Schlesien answered 25/3, 2015 at 2:19 Comment(0)
C
0

There is a setting for this in the Terminal application. Unfortunately, it is relative to all Terminal windows, not only those launched via .command file.

Coacher answered 19/4, 2010 at 18:35 Comment(2)
In Preferences, I have it set "When the shell exits: Close the window" and "Prompt before closing: Never", but it still behaves in the way I outlines above. Is there another setting that I'm missing? ThanksMoria
@Moria works for me that way, make sure you are changing settings for your default theme though.Delisadelisle
D
0

you could use some applescript hacking for this:

tell application "Terminal"
    repeat with i from 1 to number of windows
        if (number of (tabs of (item i of windows) whose tty is "/dev/ttys002")) is not 0 then
            close item i of windows
            exit repeat
        end if
    end repeat
end tell 

replacing /dev/ttys002 with your tty

Delisadelisle answered 19/4, 2010 at 18:51 Comment(0)
C
0

I'm using the following command in my script

quit -n terminal

Of course you have to have the terminal set to never prompt before closing.

Causeuse answered 27/6, 2012 at 16:33 Comment(1)
The OP mentions a shell script so you should specify your script is AppleScript.Schlesien
H
0

This combination of answers works best for me. I am using iTerm2, so I don't have the issue with quitting the whole terminal program.

Set Terminal "Ask before closing" to never

Terminal > Settings > Profiles tab > Shell > Ask before closing: never

I added a command to close the current terminal window with Command + w. Otherwise, a previous terminal window will be restored after each .command file run.

The terminal will need to have the right to send keystrokes. Terminal should ask to add this right, or you can enable it manually in macOS settings > privacy & security > accessibility > Terminal

#!/bin/bash

# ... add your code here ...

# Close the current terminal window with Command + w
osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"

# Close terminal app
osascript -e 'tell application "Terminal" to quit' &
exit

From the accepted answer: Can somebody explain why it does not work well without 'run in background' option &?

Harquebusier answered 29/8, 2023 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.