Operating System - Linux
1827809 Members
2035 Online
109969 Solutions
New Discussion

How to time stamp history file

 
joseph wholey
Regular Advisor

How to time stamp history file

Can anyone advise on a slick way to time stamp the history file?
9 REPLIES 9
Bryan Eley
Trusted Contributor

Re: How to time stamp history file

Joseph,

Maybe something like Rick Garland recommended?

HST=`hostname`
USR=`who -um | awk '{print $1}'`
NAME=`whoami`# Set History File
HISTFILE=/home/root/.sh_history_"${HST}"_"${USR}"-as-"${NAME}"_`date +%y%m%d.%
H%M%S_$$`
export HISTFILE

I can't claim to be the clever person behind this, but I found the reference in this itrc thread: http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=901609&admit=-682735245+1144174017758+28353475
Rick Garland
Honored Contributor

Re: How to time stamp history file

Thanks Bryan.

That was going to be my suggestion.

What is happening is that a new history file is created with each login session. Each of these files is time/date stamped. Since you can't get the stamps in the file for each line item, you can place a timeon the additions to the file.

joseph wholey
Regular Advisor

Re: How to time stamp history file

There is no way to append "#info info info" to each line in the .sh_history? Not even with a trap or something along those lines?
Rick Garland
Honored Contributor

Re: How to time stamp history file

What could work as an example, you could append the output of the `date` command to the $HIST file.

echo `date` >> $HIST

This will insert the date into the history file as defined by $HIST. However this is a manual process.

To automate, something in the profile might do the trick (this is a guess, not an answer)
while true
do
echo `date` >> $HIST
sleep 300
done

This little while loop will append/echo the `date` output every 5 minutes to the $HIST file.


Rick Garland
Honored Contributor

Re: How to time stamp history file

No, don't do that while loop in the profile - when you login you will not get a prompt
joseph wholey
Regular Advisor

Re: How to time stamp history file

I already tried that echo `date` >> $HIST... it writes "echo `date` >> $HIST" to the history file (unfortunately).
Mike Stroyan
Honored Contributor

Re: How to time stamp history file

With bash you can just set the HISTTIMEFORMAT variable. That causes history to display the time for each command. It also puts timestamps in the history file.

HISTTIMEFORMAT="%D %T "
joseph wholey
Regular Advisor

Re: How to time stamp history file

Is there anything similar for KSH?
Mike Stroyan
Honored Contributor

Re: How to time stamp history file

ksh doesn't have any feature intended to put timestamps in history. You can get a similar effect by combining the evaluation of the PS1 prompt and the "read -s" feature that reads into history.

PS1='$(printf "%(# %D %T )T" | read -s)$ '

This prompt setting will put the current date and time into the history each time ksh comes back to printing a prompt. The timestamp will appear as a separate line. It will show the time that the preceeding command completed. You won't be able to know exactly when a long-running command was started. (The bash timestamp feature shows the start times of entered commands and doesn't show when they finish.)