2

I have a executable bash script (.bash_history_cleaner) which cleans my bash history

#!/bin/bash
cat /dev/null > ~/.bash_history && history -c && exit

I want to execute this script on system shutdown or restart and followed this answer which uses systemd with service name as bash_history_cleaner.service and enabled it in systemctl. The service is up and running,

[Unit]
Description=Bash History Cleaner

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/eka/Scripts/.bash_history_cleaner

[Install]
WantedBy=multi-user.target

Its intended behaviour should be to clean my bash history when either shutdown or restart. But its not working, I shutdown and restared the system many times but the script is not executed. How do we execute a bash script before shutdown using systemd?

Eka
  • 3,007

1 Answers1

1

Why not take advantage of the Linux system and it's /tmp directory to store your .bash_history in a more volatile place?

To change the location of your .bash_history file, you need to set the HISTFILE variable. To have the file deleted every reboot, place the file in the /tmp/ directory.

You can add something such as the following to your ~/.bashrc file so that these settings persist:

HISTFILE=/tmp/.bash_history

You can confirm other users cannot read your history by checking out the file permissions of the history file created:

ls -lsa /tmp/.bash_history

This will cause your user's history to be removed upon reboot, but will not affect others. You can set this system level configuration files such as /etc/bash.bashrc if you really want. You can even put /tmp into RAM to ensure it gets cleared.

earthmeLon
  • 11,658