Using launchd to clear bash history?
Clarification: Am not looking to destroy
history
, am looking for means to periodically purge it vialaunchd
for security purposes
Have been using cat /dev/null > ~/.bash_history
to clear bash history as have seen in many *nix conversations as the most effective way to do so.
So I put it into a shell script that runs in launchd
, yet after some time I've come to realize though it runs it's not clearing history.
Is it possible to clear bash history this way via bash script or can it only be done manually as a user? Perhaps I need to run the command directly in the .plist and not via a bash script?
Note: I have other scripts running correctly via launchd so question is strictly about being able to clear history via an agent
Thank you
Update: Found this cool site that helps generate
launchd
scripts if this helps anyone. Cannot vouch for it but it's much easier than hacking XML. It seems to usesh -c
for all commands though, so not sure why that is.launchd plist generator http://launched.zerowidth.com/
Your bash
history consists of two "histories", not one. One of these histories is stored in a file - its size/depth is governed by the parameter HISTFILESIZE
. The other history is cached in memory - its size is governed by the parameter HISTSIZE
.
You can check the sizes/depth of your histories:
$ echo $HISTFILESIZE
$ echo $HISTSIZE
The reason for two histories is that one is associated with each shell session (HISTSIZE
), while the other history is a file ~/.bash_history
that is "permanent", and eventually receives the session histories when they overrun their max depth, or when the session is terminated. That's a bit complicated to explain, and the picture below may help make the relationship between these two histories clear.
But back to your objective:
Rather than taking steps to delete your history, you could simply set HISTSIZE
and HISTFILESIZE
to zero in ~/.bashrc
:
HISTSIZE=0
HISTFILESIZE=0
If that's not what you want, you can certainly delete the history file periodically, but know that doesn't delete the session history. Deleting your history file by building a .plist
file isn't necessary. The history
command is the tool to use for this job (see history --help
for details). The following command will delete one session history:
$ history -cw
Deleting the contents from the history file (~/.bash_history
) is done conventionally:
$ > ~/.bash_history
You could put one or both of these commands in a crontab
, but you'll need to ensure the default shell is bash
as history
is a built-in.
For some related background on the subject of shell history, you could read this related Q&A. It pertains to zsh
- not bash
, but the principles are the same.
It seems you've come up with a very convoluted solution to the problem of not wanting a bash_history.
I would advise that instead of periodically clearing the history from launchd, you should simply disable history recording:
echo 'set +o history' >> ~/.bashrc
Close the terminal tab and open a new one - and your history will no longer be recorded.