0

I like to save every history in bash to a kind of logfiles, and made an extra directory ~/lib/hists/ for that purpose.

Writing such a logfile in the shell is easy:

history -w /home/stefan/lib/hists/bash-hist-$(date +%F_%T_%N)

Now, if I shutdown, all closing xterms shall write their logfile, which is, why I include the nanotime in the filename, to avoid collision.

So I put that command into ~/.bash_logout

Unfortunately, closing an xterm didn't generate such a logfile. I thought, maybe because it's because I closed the window. So startet bash in bash:

bash
ls
exit

but again, no history written.

Most of the time I have 4 to 6 open terminals but only one history file for which I don't know, which commands are ending there - from the first opened shell, from the last closed shell? While working, the histories don't interfere, so I guess it is only bound to one shell instance.

I'm interested in explanation, why my approach fails, how I can fix it, including very different approaches.

  • 1
    This might be helpful: superuser.com/a/410534 In my testing, the shells running in xterms have not been login shells. – Andy Dalton Feb 14 '18 at 16:59
  • Are you sure you're running a login shell? Try running shopt login_shell. Edit: See Andy's link above. – aliceinpalth Feb 14 '18 at 17:00
  • @aliceinpalth: To be honest, I never understood, what a login shell is. If I hit Ctrl-Alt-F1...6, I get a shell where I have to login - are these considered login-shells? Well - with shopt I can confirm, login_shell is OFF. And I can confirm, that Andy Daltons link solved indeed my problem. – user unknown Feb 14 '18 at 17:28
  • See unix.stackexchange.com/a/49240/7696 for an example of doing something similar using /etc/bash.bash_logout. Note the caveats - in particular, it is not a secure audit log, and is not intended to be. Also, it has been some time since i wrote/used this and it may have suffered from bit-rot. – cas Feb 15 '18 at 0:44
  • @cas: Thanks. I already adopted Andy Daltons link. It needn't be secure, it's mostly for statistical purpose, and to not be worried, to close a xterm, because it might contain valueable stuff in the history. :) If I get in trouble with that solution, I know where to return to. :) – user unknown Feb 15 '18 at 1:28
1

Bash will execute the commands in .bash_logout only if the shell is a login shell.

# .bash_logout will print "hi"
$ cat .bash_logout
echo hi

# Start a new shell that is not a login shell
$ bash
$ shopt login_shell
login_shell     off

# On exit, it will not print "hi"
$ exit
exit

# Start a login shell
$ bash -l
$ shopt login_shell
login_shell     on

# On exit it will execute .bash_logout, and will print "hi"
$ exit
logout
hi

See this answer on superuser.com for more details, along with an alternative solution to the problem you're trying to solve.

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.