You can easily get a command or shell script executed when you logout of the system. This is a shell specific feature and you need to add your command or script as per your shell version:
- /bin/bash – BASH shell user create or update a file called .bash_logout in your home directory.
- /bin/csh or /bin/tcsh – CSH/TCSH shell user create or update a file called .logout in your home directory.
- Bourne / KSH Shell user need to setup a trap in .profile file as explained here.
Example: Run a command when I log out
Edit/append the following in your $HOME/.bash_logout (BASH) or $HOME/.logout (CSH/TCSH) file:
## clear the screen ## clear ## Disk info ## du -s $HOME ## Delete mysql cmd file ## /bin/rm $HOME/.mysql_history ## add rest of the stuff here for bash
A note about Bourne / KSH Shell users
Edit/append the following in your $HOME/.profile file:
trap '. $HOME/.ksh_logout; exit' 0
Save and close the file. Finally create a text file called $HOME/.ksh_logout, enter:
$ vi $HOME/.ksh_logout
Add the following:
## Disk info ## du -s $HOME # Show date ## date ## Sleep for some time so that I can read info ## sleep 5 ## clear the screen ## clear ## Delete mysql cmd file ## /bin/rm $HOME/.mysql_history ## add rest of the stuff here for ksh ##
A note about security
Please note that if you put too many commands or run a large script or script contains errors, an unauthorised person could gain access to your shell account by killing its execution. So, I recommend that you wait till you get a login prompt again. This hack will not work with GUI (KDE/Gnome/Xfce4) based login. This only works with a shell based session or ssh based session.
See man pages for more info – tcsh(1).
|Category||List of Unix and Linux commands|
|Firewall||CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|