Bash command logger

I was wondering, out of curiosity, if you can encode the bash script log of the entire command running in a Bash / SSH session. I know that I historyshould log all the teams, but it seems very unreliable!

This morning I was messing around with the following bash script that logs what the user starts in the terminal but does not execute all the commands correctly.

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

Does anyone know anything that makes teams better and more reliable than history

Greetings

+5
source share
3 answers

The reason the story seems unreliable to you is because it only records the story at the end of the BASH session, so you might lose commands.

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 history -a , ​​, . shopt -s histappend BASH , .

: http://linuxcommando.blogspot.com/2007/11/keeping-command-history-across-multiple.html

, , , BASH, HISTFILE.

+8

Here you can find a script here to write all the bash '/ embedded commands in a text file or syslog' without using a patch or special executable.

You can also write directly without syslog to the log file.

It is very easy to deploy, as it is a simple script shell that needs to be called once when initializing "bash".

+2
source

All Articles