6 ./test.sh
7 vim test.sh
8 ./test.sh
但是這里只顯示了命令,并沒有顯示執(zhí)行命令的時(shí)間,因?yàn)楸4鏆v史命令的~/.bash_history里并沒有保存時(shí)間。
通過設(shè)置環(huán)境變量 export HISTTIMEFORMAT=”%F %T `whoami` ” 給history加上時(shí)間戳
[root@servyou_web ~]# export HISTTIMEFORMAT=”%F %T `whoami` ”
[root@servyou_web ~]# history | tail
1014 2011-06-22 19:17:29 root 15 2011-06-22 19:13:02 root ./test.sh
1015 2011-06-22 19:17:29 root 16 2011-06-22 19:13:02 root vim test.sh
1016 2011-06-22 19:17:29 root 17 2011-06-22 19:13:02 root ./test.sh
1017 2011-06-22 19:17:29 root 18 2011-06-22 19:13:02 root vim test.sh
1018 2011-06-22 19:17:29 root 19 2011-06-22 19:13:02 root ./test.sh
1019 2011-06-22 19:17:29 root 20 2011-06-22 19:13:02 root vim test.sh
1020 2011-06-22 19:17:29 root 21 2011-06-22 19:13:02 root ./test.sh
1021 2011-06-22 19:17:29 root 22 2011-06-22 19:13:02 root vim test.sh
1022 2011-06-22 19:25:22 root 22 2011-06-22 19:13:02 root vim test.sh
1023 2011-06-22 19:25:28 root history | tail
可以看到,歷史命令的時(shí)間戳已經(jīng)加上了,但是.bash_history里并沒有加上這個(gè)時(shí)間戳。其實(shí)這個(gè)時(shí)間記錄是保存在當(dāng)前shell進(jìn)程內(nèi)存里的,如果你logout并且重新登錄的話會(huì)發(fā)現(xiàn)你上次登錄時(shí)執(zhí)行的那些命令的時(shí)間戳都為同一個(gè)值,即當(dāng)時(shí)logout時(shí)的時(shí)間。
盡管如此,對(duì)于加上screen的bash來說,這個(gè)時(shí)間戳仍然可以長(zhǎng)時(shí)間有效的,畢竟只要你的server不重啟,screen就不會(huì)退出,因而這些時(shí)間就能長(zhǎng)時(shí)間保留。你也可以使用echo ‘export HISTTIMEFORMAT=”%F %T `whoami` “‘ >> /etc/profile 然后source一下就OK