进程退出时如何自动关闭macOS终端

MacOS Terminal Stock Lede

If you’re a fan of shell scripts in macOS, you’ve probably noticed how running one will leave you with a useless Terminal window after it’s completed. You can fix this from the Terminal settings.

如果您是macOS中shell脚本的狂热者,您可能已经注意到运行它会在完成后如何为您留下无用的终端窗口。 您可以从终端设置中解决此问题。

This method only works if you’re launching a shell script outside of bash, such as by clicking on it in Finder or by setting a custom hotkey to open the program. Otherwise, you’ll just be taken back to the command prompt. You can always use killall Terminal to make the Terminal app close itself from within a script, but that will close every open Terminal window, so it’s not ideal.

仅当您在bash之外启动shell脚本时(例如,通过在Finder中单击它或通过设置自定义热键来打开该程序),此方法才有效。 否则,您将直接回到命令提示符。 您始终可以使用killall Terminal使Terminal应用程序从脚本中自行关闭,但这会关闭每个打开的Terminal窗口,因此并不理想。

在配置文件设置中更改此行为 (Change This Behaviour in the Profile Settings)

Open up the Terminal app from the Dock or your Applications folder, then open the settings by pressing Command+Comma.

从Dock或“应用程序”文件夹中打开“终端”应用程序,然后按Command +逗号以打开设置。

In the Settings window, switch to the Profiles tab. The default profile (the one at the top) should be selected by default. In the settings on the right, click the “Shell” tab and then click the “When the shell exits” drop-down menu.

在“设置”窗口中,切换到“配置文件”选项卡。 默认情况下应选择默认配置文件(顶部的配置文件)。 在右侧的设置中,单击“外壳”选项卡,然后单击“外壳退出时”下拉菜单。

The drop-down menu defaults to “Don’t close the window,” but you’ll want to change this to “Close if the shell exited cleanly.”

下拉菜单默认为“不关闭窗口”,但您需要将其更改为“如果外壳干净退出,则关闭”。

MacOS Terminal Close on process exit

You can also make it close every time, but this way you’ll still get an error message if a process exits with a nonzero exit status. Keep in mind that you may need to exit the script explicitly with the exit command to get this behavior in all cases.

您也可以每次关闭它,但是如果进程以非零退出状态退出,则仍然会收到错误消息。 请记住,在所有情况下,您可能都需要使用exit命令显式退出脚本才能获得此行为。

Shell script exit command

Though if you’re launching from Finder, the exit command is automatically appended to the script.

尽管如果从Finder启动,则exit命令会自动附加到脚本中。

或者,使用iTerm (Alternatively, Use iTerm)

iTerm2 settings

iTerm2, a popular Terminal replacement for macOS, will automatically close the window when a shell script exits. If you already use iTerm as your default terminal, you may have noticed that shell scripts launched from Finder still open with the stock Terminal app. This means you’ll still have the same issue unless you set scripts to open with iTerm.

iTerm2是macOS的一种流行的Terminal替代品,在退出Shell脚本时会自动关闭窗口。 如果您已经使用iTerm作为默认终端,则可能已经注意到从Finder启动的shell脚本仍会随库存的Terminal应用程序一起打开。 这意味着除非将脚本设置为使用iTerm打开,否则仍然会遇到相同的问题。

You can change which applications scripts open in by right clicking on the script in Finder and then selecting “Get Info.”

通过右键单击Finder中的脚本,然后选择“获取信息”,可以更改打开哪些应用程序脚本。

macOS open script with iTerm

There will be a dropdown to change what this script opens with. Set it to iTerm and press “Change All” to apply this change to every script.

将有一个下拉菜单来更改此脚本的打开方式。 将其设置为iTerm,然后按“全部更改”以将此更改应用于每个脚本。

By default, if you already have an iTerm window open, it will launch in a separate tab rather than a separate window, and the tab will close automatically when it’s done.

默认情况下,如果您已经打开了iTerm窗口,它将在一个单独的选项卡而不是一个单独的窗口中启动,并且该选项卡在完成后会自动关闭。

翻译自: https://www.howtogeek.com/409849/how-to-close-the-macos-terminal-automatically-when-a-process-exits/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值