Linux之history记录时间戳

运维需求:要在linux操作系统中查看history记录的操作时间

解决:在/etc/profile或者用户家目录.bash_profile的文件末尾添加一行配置,并使配置生效

export HISTTIMEFORMAT="%F %T `whoami` "

方便日后批量化添加管理:

#########################################################################
# File Name: add_history_time.sh
# Author: mads
# Mail: 1455975151@qq.com
# Created Time: Tue 19 Jul 2016 02:17:46 AM CST
# Description : this is scripts use to
# Version : v1.0
#########################################################################
#!/bin/bash

. /etc/init.d/functions

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

add_history_time(){
grep HISTTIMEFORMAT /etc/profile >/dev/null 2>&1
if [ ! $? -eq 0 ]
then
    echo -e "${GREEN_COLOR}add history time to /etc/profile${RES}"
    echo -e "\n# history add time by mads\nexport HISTTIMEFORMAT=\"%F %T \`whoami\` \"">>/etc/profile
    source /etc/profile
else
    echo -e "${RED_COLOR}add history time to /etc/profile done${RES}"
fi
}

add_hostname(){
grep workspace /etc/profile >/dev/null 2>&1
if [ ! $? -eq 0 ]
then
    if [ -f /usr/local/workspace/agent/cfg.json ]
    then
        echo -e "${GREEN_COLOR}add echo IP setup to /etc/profile${RES}"
        cat >>/etc/profile<<EOF

# echo IP in bash by mads at `date +%Y%m%d`
IP=\`awk -F '[-"]+' '/hostname/{print \$(NF-1)}' /usr/local/workspace/agent/cfg.json\`
PS1='[\u@\$IP \W]\$ '
EOF
    else
        echo -e "${RED_COLOR}/usr/local/workspace/agent/cfg.json is not exist${RES}"
    fi
else
   echo -e "${RED_COLOR}add echo IP setup to /etc/profile is done${RES}"
fi
}

main(){
add_history_time
add_hostname
}

main

 

posted @ 2016-08-05 10:46  每天进步一点点!!!  阅读(1089)  评论(0编辑  收藏  举报