有没有办法知道当前 bash 会话中的 `script` 命令是否为 运行?
Is there a way to know if the `script` command is running in the current bash session?
我正在尝试向我的 ~/.zshrc
文件添加一个命令来记录命令行输入和输出。我有 script
(https://www.tecmint.com/record-and-replay-linux-terminal-session-commands-using-script/) 命令需要 运行ning 但我遇到了一个问题,它似乎无限地尝试记录我的会话。我相信这是因为当您 运行 script
命令时,它会启动一个新的 bash 会话,然后 运行s ~/.zshrc
然后尝试记录重新启动会话的会话(等等)。这会导致它进入尝试记录会话的无限循环。
尝试 1
我的相关作品 ~/.zshrc
# RECORD COMMAND INPUT AND OUTPUT
LOG_PATH=/var/log/terminal/$(date +'%Y%m%d')
LOG_FILE=${LOG_PATH}/$(date +'%H%M%S').log
mkdir -p ${LOG_PATH}
script ${LOG_FILE}
这会导致会话不断打印以下内容:
Script started, output file is /var/log/terminal/20200109/141849.log
Script started, output file is /var/log/terminal/20200109/141850.log
Script started, output file is /var/log/terminal/20200109/141851.log
Script started, output file is /var/log/terminal/20200109/141852.log
Script started, output file is /var/log/terminal/20200109/141853.log
Script started, output file is /var/log/terminal/20200109/141854.log
Script started, output file is /var/log/terminal/20200109/141855.log
... repeat infinitely ...
尝试 2
解决此问题的一种尝试是检查文件是否已经存在,然后继续录制(这有点管用,但有时它会创建 2 个文件,或者有时录制不会开始说如果我快速连续打开 2 个命令会话)。
升级脚本
# RECORD COMMAND INPUT AND OUTPUT
# Check if the recording of a file for the given time has already started
# as it seems that once you start the script recording it re-starts the session
# which in turn re-runs this file which attempts to script again running into an infinite loop
LOG_PATH=/var/log/terminal/$(date +'%Y%m%d')
LOG_FILE=${LOG_PATH}/$(date +'%H%M%S').log
if [[ ! -f $LOG_FILE ]]; then
mkdir -p ${LOG_PATH}
script ${LOG_FILE}
fi
同样,这要么不会产生任何记录(在快速打开 2 个会话的情况下),要么会导致记录完成两次
Script started, output file is /var/log/terminal/20200109/141903.log
Script started, output file is /var/log/terminal/20200109/141904.log
尝试 3
另一个尝试是检查 bash 历史并查看最后一个命令是否包含单词 script
。此尝试的问题是 bash 会话在启动时似乎没有历史记录,因此出现以下错误(然后像第一次尝试一样无限地尝试启动记录会话):
# RECORD COMMAND INPUT AND OUTPUT
# Check what the last command was to make sure that it wasn't the script starting
# as it seems that once you start the script recording it re-starts the session
# which in turn re-runs this file which attempts to script again running into an infinite loop
LAST_HISTORY=$(history -1)
if [[ "$LAST_HISTORY" != *"script"* ]]; then
LOG_PATH=/var/log/terminal/$(date +'%Y%m%d')
LOG_FILE=${LOG_PATH}/$(date +'%H%M%S').log
mkdir -p ${LOG_PATH}
script ${LOG_FILE}
fi
输出
Last login: Thu Jan 9 14:27:30 on ttys009
Script started, output file is /var/log/terminal/20200109/142754.log
Script started, output file is /var/log/terminal/20200109/142755.log
omz_history:fc:13: no such event: 0
omz_history:fc:13: no events in that range
Script started, output file is /var/log/terminal/20200109/142755.log
omz_history:fc:13: no such event: 0
omz_history:fc:13: no events in that range
Script started, output file is /var/log/terminal/20200109/142755.log
Script started, output file is /var/log/terminal/20200109/142756.log
^C% danielcarmo@Daniels-MacBook-Pro-2 git %
如有任何建议或想法,我们将不胜感激。
与其试图弄清楚 script
是否是 运行,不如为自己留下一个面包屑:
if [ -z "$BEING_LOGGED" ]
then
export BEING_LOGGED="yes"
LOG_PATH="/var/log/terminal/$(date +'%Y%m%d')"
LOG_FILE="${LOG_PATH}/$(date +'%H%M%S').log"
mkdir -p "${LOG_PATH}"
exec script "${LOG_FILE}"
fi
第一次获取文件时,BEING_LOGGED
将被取消设置。当调用 script
并再次获取文件时,它将被设置,您可以跳过日志记录。
script
将变量 SCRIPT
添加到命令 it 运行s 的环境中。您可以检查一下以确定 script
是否需要 运行.
# RECORD COMMAND INPUT AND OUTPUT
log_path=/var/log/terminal/$(date +'%Y%m%d')
log_file=${log_path}/$(date +'%H%M%S').log
mkdir -p "${log_path}"
<b>[ -z "$SCRIPT" ] && </b>script "${log_file}"
SCRIPT
的值是要记录到的文件的名称。
我正在尝试向我的 ~/.zshrc
文件添加一个命令来记录命令行输入和输出。我有 script
(https://www.tecmint.com/record-and-replay-linux-terminal-session-commands-using-script/) 命令需要 运行ning 但我遇到了一个问题,它似乎无限地尝试记录我的会话。我相信这是因为当您 运行 script
命令时,它会启动一个新的 bash 会话,然后 运行s ~/.zshrc
然后尝试记录重新启动会话的会话(等等)。这会导致它进入尝试记录会话的无限循环。
尝试 1
我的相关作品 ~/.zshrc
# RECORD COMMAND INPUT AND OUTPUT
LOG_PATH=/var/log/terminal/$(date +'%Y%m%d')
LOG_FILE=${LOG_PATH}/$(date +'%H%M%S').log
mkdir -p ${LOG_PATH}
script ${LOG_FILE}
这会导致会话不断打印以下内容:
Script started, output file is /var/log/terminal/20200109/141849.log
Script started, output file is /var/log/terminal/20200109/141850.log
Script started, output file is /var/log/terminal/20200109/141851.log
Script started, output file is /var/log/terminal/20200109/141852.log
Script started, output file is /var/log/terminal/20200109/141853.log
Script started, output file is /var/log/terminal/20200109/141854.log
Script started, output file is /var/log/terminal/20200109/141855.log
... repeat infinitely ...
尝试 2 解决此问题的一种尝试是检查文件是否已经存在,然后继续录制(这有点管用,但有时它会创建 2 个文件,或者有时录制不会开始说如果我快速连续打开 2 个命令会话)。
升级脚本
# RECORD COMMAND INPUT AND OUTPUT
# Check if the recording of a file for the given time has already started
# as it seems that once you start the script recording it re-starts the session
# which in turn re-runs this file which attempts to script again running into an infinite loop
LOG_PATH=/var/log/terminal/$(date +'%Y%m%d')
LOG_FILE=${LOG_PATH}/$(date +'%H%M%S').log
if [[ ! -f $LOG_FILE ]]; then
mkdir -p ${LOG_PATH}
script ${LOG_FILE}
fi
同样,这要么不会产生任何记录(在快速打开 2 个会话的情况下),要么会导致记录完成两次
Script started, output file is /var/log/terminal/20200109/141903.log
Script started, output file is /var/log/terminal/20200109/141904.log
尝试 3
另一个尝试是检查 bash 历史并查看最后一个命令是否包含单词 script
。此尝试的问题是 bash 会话在启动时似乎没有历史记录,因此出现以下错误(然后像第一次尝试一样无限地尝试启动记录会话):
# RECORD COMMAND INPUT AND OUTPUT
# Check what the last command was to make sure that it wasn't the script starting
# as it seems that once you start the script recording it re-starts the session
# which in turn re-runs this file which attempts to script again running into an infinite loop
LAST_HISTORY=$(history -1)
if [[ "$LAST_HISTORY" != *"script"* ]]; then
LOG_PATH=/var/log/terminal/$(date +'%Y%m%d')
LOG_FILE=${LOG_PATH}/$(date +'%H%M%S').log
mkdir -p ${LOG_PATH}
script ${LOG_FILE}
fi
输出
Last login: Thu Jan 9 14:27:30 on ttys009
Script started, output file is /var/log/terminal/20200109/142754.log
Script started, output file is /var/log/terminal/20200109/142755.log
omz_history:fc:13: no such event: 0
omz_history:fc:13: no events in that range
Script started, output file is /var/log/terminal/20200109/142755.log
omz_history:fc:13: no such event: 0
omz_history:fc:13: no events in that range
Script started, output file is /var/log/terminal/20200109/142755.log
Script started, output file is /var/log/terminal/20200109/142756.log
^C% danielcarmo@Daniels-MacBook-Pro-2 git %
如有任何建议或想法,我们将不胜感激。
与其试图弄清楚 script
是否是 运行,不如为自己留下一个面包屑:
if [ -z "$BEING_LOGGED" ]
then
export BEING_LOGGED="yes"
LOG_PATH="/var/log/terminal/$(date +'%Y%m%d')"
LOG_FILE="${LOG_PATH}/$(date +'%H%M%S').log"
mkdir -p "${LOG_PATH}"
exec script "${LOG_FILE}"
fi
第一次获取文件时,BEING_LOGGED
将被取消设置。当调用 script
并再次获取文件时,它将被设置,您可以跳过日志记录。
script
将变量 SCRIPT
添加到命令 it 运行s 的环境中。您可以检查一下以确定 script
是否需要 运行.
# RECORD COMMAND INPUT AND OUTPUT
log_path=/var/log/terminal/$(date +'%Y%m%d')
log_file=${log_path}/$(date +'%H%M%S').log
mkdir -p "${log_path}"
<b>[ -z "$SCRIPT" ] && </b>script "${log_file}"
SCRIPT
的值是要记录到的文件的名称。