Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to be able to setup a MySQL database and record all commands issued on my server by username and command. Is this possible, and if so, how would I set it up?

share|improve this question
do you want this in real time or periodically updated - for example once a day? I would suggest regular review of users history file but it will left you with possibility of users removing specific entries from their history files. But there is a tool called script and it can log all commands to a txt file. You can add script command to users .profile file and restrict the editing of this file. – mnmnc Dec 17 '12 at 14:17

1 Answer

You could

  • play with PROMPT_COMMAND if you want this in real-time (each line), this could be something like:

    PROMPT_COMMAND='echo mysql -c "INSERT $(history 1|sed "s/^ *[0-9]* *//")"'
    

    this could be stored in your $HOME/.bashrc.

  • play with $HOME/.bash_logout if you prefer digest at each logout. this could let you begin your own script, try adding something like this to your .profile

    if [ "$HISTFILE" ] ;then
        export OHISTFILE="$HISTFILE"
        export HISTFILE=$(mktemp $HOME/.bash_history_XXXXXXXX)
        touch $HISTFILE
        history -a
        cat >>"$OHISTFILE" "$HISTFILE"
        HISTFILE="$OHISTFILE"
        HISTFILESIZE=$HISTFILESIZE
        mysql -c "INSERT ... $(<$HISTFILE).."
        rm $HISTFILE
      fi
    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.