OS X终端会话如何通过重新引导持续存在?


14

作为购买MacBook Pro之前的狂热Linux用户,我通常会同时打开多个终端选项卡。

过去,崩溃和重新启动通常会浪费我的工作流程以及我各自的大多数制表历史。我一直在寻找解决这个问题的方法,但总是空虚。除了各种技术,工具使用的组合喜欢sshscreentmux,和所需的虚拟专用服务器(或类似)。

我最喜欢的事情之一是使用MacBook Pro编写脚本以及使用CLI工具等。是我的终端会话在崩溃之前仍然存在,并且默认情况下会重新启动。实际上,我刚刚恢复了将近2年前的备份,并且当我第一次登录时,我看到了我的旧台式机和三个bash外壳,其中包括我一直在从事的项目。

我想知道OS X如何使此功能成为可能。这里有人对它的工作原理有见识吗?

Answers:


10

该代码恢复终端(实际上bash会话)是的一部分,/etc/bashrc_Apple_Terminal其中获得通过采购/etc/profile/etc/bashrc每个bash在终端运行的会话。

# Resume Support: Save/Restore Shell State
#
# Terminal assigns each terminal session a unique identifier and
# communicates it via the TERM_SESSION_ID environment variable so that
# programs running in a terminal can save/restore application-specific
# state when quitting and restarting Terminal with Resume enabled.
#
# The following code defines a shell save/restore mechanism. Users can
# add custom state by defining a shell_session_save_user_state function
# that writes restoration commands to the session file at exit. e.g.,
# to save a variable:
#
#   shell_session_save_user_state() { echo MY_VAR="'$MY_VAR'" >> "$SHELL_SESSION_FILE"; }
#
# During shell startup the session file is executed. Old files are
# periodically deleted.
#
# The default behavior arranges to save and restore the bash command
# history independently for each restored terminal session. It also
# merges commands into the global history for new sessions. Because
# of this it is recommended that you set HISTSIZE and HISTFILESIZE to
# larger values.
#
# You may disable this behavior and share a single history by setting
# SHELL_SESSION_HISTORY to 0. There are some common user customizations
# that arrange to share new commands among running shells by
# manipulating the history at each prompt, and they typically include
# 'shopt -s histappend'; therefore, if the histappend shell option is
# enabled, per-session history is disabled by default. You may
# explicitly enable it by setting SHELL_SESSION_HISTORY to 1.
#
# The implementation of per-session command histories in combination
# with a shared global command history is incompatible with the
# HISTTIMEFORMAT variable--the timestamps are applied inconsistently
# to different parts of the history; therefore, if HISTTIMEFORMAT is
# defined, per-session history is disabled by default.
#
# Note that this uses PROMPT_COMMAND to enable per-session history
# the first time for each new session. If you customize PROMPT_COMMAND
# be sure to include the previous value. e.g.,
#
#   PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }your_code_here"
#
# Otherwise, the per-session history won't take effect until the first
# restore.
#
# The save/restore mechanism is disabled if the following file exists:
#
#   ~/.bash_sessions_disable

1
太酷了,这些评论来自/etc/bashrc_Apple_Terminal吗?我特别喜欢# The default behavior arranges to save and restore the bash command history independently for each restored terminal session. It also # merges commands into the global history for new sessions.那是我以前尝试实现的其他东西,但无济于事。
声音

因此,无论如何,我都想将其标记为答案,但是我一直在阅读此文件,并且..您能指出引起这种效果的特定代码行吗?似乎不仅仅是注释中提到的功能。可能只是我疲倦的眼睛,但我对此没有多大意义。
声音

@ tjt263从未发现时间可以解决这个问题
nohillside

@ tjt263实际上是从注释到文件末尾的所有内容。基本上,它用于trap捕获会话结束并将其历史记录存储在选项卡/特定于会话的文件中。
nohillside

7

据我所知,它只是将文本保存在每个窗口的回滚缓冲区中。它实际上并没有保存终端中正在运行的状态。它只是在重启后启动一个新的shell。

作为实验,请在您的shell中定义一个变量,然后检查其值:

foo=bar
echo $foo

然后重新启动,然后再次检查变量的值。您会看到它不再定义。


ew!否则,那将是令人毛骨悚然的。
uhoh
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.