OSX - How to auto Close Terminal window after the

2020-02-07 15:49发布

When I'm done with Terminal, I want to exit it. Right now, I have three options:

  1. killall Terminal. It will end the process, but rather abruptly. I don't think this is the best idea.

  2. Call exit. I've changed the settings so exit closes Terminal. The app still appears open in the Dock though, and doesn't do what I want it to do.

  3. Right click>Quit. However, this isn't a Terminal command to exit it.

So now, what is the way I should exit and close Terminal? I've heard something about osascript but I'm not too sure. I want to exit and close, so that Terminal is no longer open, both as a window and as a process.

13条回答
在下西门庆
2楼-- · 2020-02-07 16:42

In a terminal window, you can type:

kill -9 $(ps -p $PPID -o ppid=)

This will kill the Terminal application process, which is the parent of the parent of the current process, as seen by the kill command.

To close a Terminal window from within a running script, you need to go up one more level in the process hierarchy like this:

kill -9 $(ps -p $(ps -p $PPID -o ppid=) -o ppid=) 
查看更多
干净又极端
3楼-- · 2020-02-07 16:42

I've been using

quit -n terminal

at the end of my scripts. You have to have the terminal set to never prompt in preferences

So Terminal > Preferences > Settings > Shell When the shell exits Close the window Prompt before closing Never

查看更多
淡お忘
4楼-- · 2020-02-07 16:45

If this is a Mac you type 'exit' then press return.

查看更多
▲ chillily
5楼-- · 2020-02-07 16:46

In the Terminal app, Preference >> Profiles tab.

Select the Shell tab on the right. enter image description here

You can choose Never Ask before closing to suppress the warning.

查看更多
\"骚年 ilove
6楼-- · 2020-02-07 16:46

Actually, you should set a config on your Terminal, when your Terminal is up press +, then you will see below screen:

enter image description here

Then press shell tab and you will see below screen:

enter image description here

Now select Close if the shell exited cleanly for When the shell exits.

By the above config each time with exit command the Terminal will close but won't quit.

查看更多
孤傲高冷的网名
7楼-- · 2020-02-07 16:46

osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"

This simulates a CMD + w keypress.

If you want Terminal to quit completely you can use: osascript -e "tell application \"System Events\" to keystroke \"q\" using command down"

This doesn't give any errors and makes the Terminal stop cleanly.

查看更多
登录 后发表回答