How to Run and Execute Command When I Log Out Of Linux Session?

I‘m using CentOS 7 server. I need to run a command when I logout using ssh or bash shell. So in Linux and Unix-like systems, how can I run a command to execute when I log out my session?

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:
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements Bash/CSH/TCSH
Time 2m
  1. /bin/bash – BASH shell user create or update a file called .bash_logout in your home directory.
  2. /bin/csh or /bin/tcsh – CSH/TCSH shell user create or update a file called .logout in your home directory.
  3. 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 ##
## 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 ##
## Sleep for some time so that I can read info ##
sleep 5
## clear the screen ##
## 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).

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Colin Feb 13, 2015 @ 13:34

    You could wrap it up in your .profile (or .bash_profile, etc.). Something like

    . echo “You are now logging out at “`date`

    It is important that the last command is run in place of the shell (hence the “. ” at the start of the line) or you will have to effectively log out twice.

  • Franjo Mar 16, 2015 @ 13:22

    Every bit of code is working as written when using exit command.
    But !
    When I close Putty or terminal window and answer yes to question Are you shure …, or Do you want … the window closes and .bash_logout is not processed.
    I need to delete .Xauthority file no metter how do I get out of ssh session!

    Regards, Franjo

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @