System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

How can i enable time stamp in the history output.

 
Jestin John Chacko
Regular Advisor

How can i enable time stamp in the history output.

Hai....

Is it possible to add time stamp(IE,at what time the particular command was executed) in the history output.

if yes how can i do it,Is there any script for the same...


Thanks & Regards

Jestin
7 REPLIES
Michael Steele_2
Honored Contributor

Re: How can i enable time stamp in the history output.

Hi
You can't.

But, you can add this to each .profile file that you want a history on:

vi /home/user/.profile
script > /home/user/history 2>&1
Support Fatherhood - Stop Family Law
James R. Ferguson
Acclaimed Contributor

Re: How can i enable time stamp in the history output.

Hi Jestin:

If you want to add the Bash shell to your server (and DO NOT make it the default shell for 'root' as this will render your server inoperable on the next boot), then you can have timestamps in your history.

Set the Bash HISTTIMEFORMAT environmental variable. See the Bash documentation.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: How can i enable time stamp in the history output.

Hi (again) Jestin:

I should be more clear. In Bash, in '.bash_profile' you can do:

# HISTTIMEFORMAT='%m%d-%H%M%S: '

...or whatever you want the timestamp format to be. The formatting directives are like those you would use for 'date' (in reality they are from 'strftime()').

Regards!

...JRF...
Hakki Aydin Ucar
Honored Contributor

Re: How can i enable time stamp in the history output.

yes bash script needed, due to this limitation in generally need some trick in HP-UX ,but it is possible in linux,
for example I use it in linux RedHat :

# export HISTTIMEFORMAT=â %F %T â
# history | more
1 2008-08-05 19:02:39 service network restart 2 2008-08-05 19:02:39 exit 3 2008-08-05 19:02:39 id

Hakki Aydin Ucar
Honored Contributor

Re: How can i enable time stamp in the history output.

>Michael: script > /home/user/history 2>&1

If I did not get mistakenly your concept , it does not work in Posix , just got stuck due to script activation..
Mark S Meadows
Valued Contributor

Re: How can i enable time stamp in the history output.

Hi Jestin,

Try adding the following lines to your .profile and re-accessing it :

#trap to timestamp commands
trap 'date "+# %c" | read -s' debug

Then you should see something similar to the following in your history file :

# Mon Oct 5 16:50:13 2009
ls -ail
# Mon Oct 5 16:50:18 2009
bdf
# Mon Oct 5 16:50:21 2009
tail /.sh_history_markme
# Mon Oct 5 16:50:32 2009
vi .profile

Regards,

Mark
Administrating HP-UX systems for more years than I care to admit, but still enjoying it (most of the time!).
Bill Hassell
Honored Contributor

Re: How can i enable time stamp in the history output.

Here is another technique that does not involve changing any profiles:

#!/usr/bin/sh
set -u
for ACTIVE in $(who -q | grep -v users)
do
echo "## $(date)" >> ~$ACTIVE/.sh_history
done

This script uses who -q to get just a list of logged-in users, then appends the current date-time to .sh_history in their home directory. You can save this as a script and put it into root's cron to run a few times during the day.

Now everyone's .sh_history will be time stamped while they are logged in.


Bill Hassell, sysadmin