我想知道,出于好奇,如果可以编码bash脚本,则记录在Bash / SSH会话中运行的所有命令.我知道历史记录是假设记录所有运行的命令,但它似乎非常不可靠!
我今天早上一直在搞乱,并提出了以下bash脚本,它记录了用户在终端中运行的内容,但没有正确运行所有命令.
prompt_read() {
echo -n “$(whoami)@$(hostname):$(pwd)~$“
read userinput
}
prompt_read
while :; do
if [[ $userinput != exit ]]; then
logger "logit $userinput"
bash -c "$userinput"
prompt_read
else
kill -1 $PPID
fi
done
是否有人知道任何记录命令比历史记录更好,更可靠的东西
干杯
最佳答案
历史似乎不可靠的原因是因为它只会在BASH会话结束时写入历史记录,因此您可能会丢失命令.
我的bash配置文件中有一些内容:
HISTFILESIZE=10000 # how many lines of history to store in the history file
HISTSIZE=10000 # how many lines of history to store in a session ( I think )
HISTCONTROL=ignoredups # ignore duplicate commands
shopt -s histappend # append history,rather than having sessions obliterate existing history
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
最后几个是重要的,将PROMPT_COMMAND设置为历史记录 – 会立即追加历史记录,而不是会话后追加.并设置shopt -s histappend将使bash会话附加到历史文件,而不是覆盖现有的历史记录.
更多信息:http://linuxcommando.blogspot.com/2007/11/keeping-command-history-across-multiple.html
此外,如果这对您有用,则可以使用HISTFILE环境变量更改用于特定bash会话的历史记录文件的名称.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。