redhat

How To Make Writing to .bash_history Immediate

In Red Hat Linux, this is accomplished by adding a small script to the system’s profile.d directory. It is possible to edit existing files like bashrc but you may lose those changes when upgrading. I usually create a custom profile script that contains this as well as other global variables and place it at /etc/profile.d. For this post, we’ll create global-prof.sh and get this setup.

It’s best if you login or su as root before you begin.

Create the file for the script

vim /etc/profile.d/global-prof.sh

Add this to the script

#!/bin/bash

# Location of this script: /etc/profile.d/

# Purpose of script is to makes writing to .bash_history immediate
shopt -s histappend
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"

# End of script

Save the script and exit by typing

:wq!

and press Enter. This writes and quits out of the vim editor.

Set permissions and ownership of file

chown root:root /etc/profile.d/global-prof.sh
chmod 644 /etc/profile.d/global-prof.sh

Make the script executable

chmod +x /etc/profile.d/global-prof.sh

Log completely out, log back in, run a couple of commands (any commands like history, ls, etc.), and then check the history by running:

cat .bash_history

You should see the last couple of commands you just ran.

 

A quick break-down of the script itself

The first command changes the .bash_history file mode to append. And the second configures the ‘history -a’ command to be run at the shell. The ‘-a’ immediately writes the current/new lines to the .bash_history file.

Posted in Bash Shell, Linux SysAdmin, Scripting, Scripts and tagged , , , , , , , , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *


*