Rai*_*Son 5 linux bash terminal logging
我想知道,出于好奇,如果可以编码bash脚本,则记录在Bash/SSH会话中运行的所有命令.我知道history
假设记录所有运行的命令,但它似乎非常不可靠!
我今天早上一直在搞乱,并提出了以下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
Run Code Online (Sandbox Code Playgroud)
是否有人知道任何更好,更可靠地记录命令的东西 history
干杯
历史似乎不可靠的原因是因为它只会在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"
Run Code Online (Sandbox Code Playgroud)
最后几个是重要的,设置你PROMPT_COMMAND
的history -a
会立即追加历史,而不是会后.并且设置shopt -s histappend
将使bash会话附加到历史文件,而不是覆盖现有历史记录.
更多信息:http://linuxcommando.blogspot.com/2007/11/keeping-command-history-across-multiple.html
此外,如果这对您有用,则可以使用HISTFILE
环境变量更改用于特定bash会话的历史记录文件的名称.