0

There are two lines in regular user .bashrc(debian8).

cat /home/debian8/.bashrc

export HISTTIMEFORMAT="%F %T   `tty` "
export PROMPT_COMMAND="history -w"

At the first login with user debian8 and to input tty pwd,not to close it . enter image description here

At the second login with user debian8 and to input tty ls. enter image description here

Now to reboot pc and get log info with history command.
What i get is as below:

debian8@hwy:~$ history
    1  2017-03-02 22:48:25   /dev/pts/0 tty
    2  2017-03-02 22:48:28   /dev/pts/0 pwd
    3  2017-03-02 22:48:38   /dev/pts/0 tty
    4  2017-03-02 22:48:40   /dev/pts/0 ls
    5  2017-03-02 22:48:38   /dev/pts/0 tty
    6  2017-03-02 22:48:40   /dev/pts/0 ls
    7  2017-03-02 22:48:25   /dev/pts/0 tty
    8  2017-03-02 22:48:28   /dev/pts/0 pwd
    9  2017-03-02 22:48:55   /dev/pts/0 history

Why can't get the info such as following?How to get the following log info?

debian8@hwy:~$ history
    1  2017-03-02 22:48:25   /dev/pts/0 tty
    2  2017-03-02 22:48:28   /dev/pts/0 pwd
    3  2017-03-02 22:48:38   /dev/pts/1 tty
    4  2017-03-02 22:48:40   /dev/pts/1 ls
    5  2017-03-02 22:48:55   /dev/pts/0 history

To change from

export HISTTIMEFORMAT="%F %T   `tty` "
export PROMPT_COMMAND="history -w"

into

export HISTTIMEFORMAT="%F %T   `tty` "
export PROMPT_COMMAND="history -a"

not '%F %T tty '.

What i get is

debian8@hwy:~$ history
    1  2017-03-02 22:48:25   /dev/pts/0 tty
    2  2017-03-02 22:48:28   /dev/pts/0 pwd
    3  2017-03-02 22:48:38   /dev/pts/0 tty
    4  2017-03-02 22:48:40   /dev/pts/0 ls
    5  2017-03-02 22:48:55   /dev/pts/0 history

Is there no way to get such info as following?

debian8@hwy:~$ history
    1  2017-03-02 22:48:25   /dev/pts/0 tty
    2  2017-03-02 22:48:28   /dev/pts/0 pwd
    3  2017-03-02 22:48:38   /dev/pts/1 tty
    4  2017-03-02 22:48:40   /dev/pts/1 ls
    5  2017-03-02 22:48:55   /dev/pts/0 history

migrated from stackoverflow.com Mar 3 '17 at 15:35

This question came from our site for professional and enthusiast programmers.

0

My solution is an actual workaround by setting the PROMPT_COMMAND environment variable in ~/.bash_profile or in ~/.bashrc which creates an extra log.

The file ~/.bash_history only keeps a timestamp and the command like this:

#1488631365
ls -lah
#1488631367
history
#1488631380
cat ~/.bash_history

the HISTTIMEFORMAT environment variable is only the setting how the #1488631380 should be translated/displayed.


The following would create todays logfile - ~/bash-history-2017-03-04.log

export PROMPT_COMMAND='echo -e "$(tty)\t$(history 1)" >> ~/bash-history-$(date "+%Y-%m-%d").log' 

example output (depending how HISTTIMEFORMAT is setup)

/dev/pts/1        541  [04/03 14:02:37] tty
/dev/pts/1        542  [04/03 14:02:45] echo "hi from tty1"
/dev/pts/0        523  [04/03 13:55:07] cat ~/.bash_history
/dev/pts/0        524  [04/03 14:02:53] tty
/dev/pts/0        525  [04/03 14:02:58] echo "hi from tty 0"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.