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
出处:http://www.cnblogs.com/madsnotes/
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。