Linux / UNIX: Run Commands When You Log Out

I’ve written a Perl script that connects to our central server for me and it allows me feed data so that I make a timesheet later. How do I run my script when I log out from Apple OS X or Linux / UNIX workstation using bash shell?

Almost all modern shell including bash allows you run run command when you log out. Typically this is used to:

ADVERTISEMENTS

  1. Clean up screen with clear command.
  2. Remove history and other temporary file.
  3. Run commands or scripts and so on.

Logout file name

Commands in .logout are run when you log out.

  1. bash shell: ~/.bash_logout
  2. tcsh / csh: ~/.logout

Edit $HOME/.bash_logout and add your command:
$ vi ~/.bash_logout
Sample logout configuration:

if [ "$SHLVL" = 1 ]; then
    #clear screen
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
 
    # delete mysql history
    [ -f $HOME/.mysql_history ] && /bin/rm $HOME/.mysql_history
 
    # Update ip accounting
    [ -x /usr/bin/ip_accouting ] && /usr/bin/ip_accouting -u "$USER" -a
 
   # call your script here
   [ -x /usr/local/bin/timesheet_client.pl ] && /usr/local/bin/timesheet_client.pl
fi

A Note About Old Shell And Bourne / KSH Shell

Older UNIX shell and the Bourne / ksh shell don’t have a logout file. So edit ~/.profile file, enter:
$ vi ~/.profile
Next append the following line:

trap '. $HOME/.my_shell_logout; exit' 0

Save and close the file. Finally create $HOME/.my_shell_logout, enter:
$ vi $HOME/.my_shell_logout
Sample config

   # call your script here
  if [ -f /usr/local/bin/timesheet_client.pl ] 
  then 
    /usr/local/bin/timesheet_client.pl
  fi
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Pat Dec 12, 2013 @ 21:44

    Thank you. I was trying to figure out how to execute my cleanup procedure when I logged out of my session. I have ksh, so this was a huge help. Thanks very much.

  • Eric Sebasta Jun 5, 2014 @ 1:33

    This only works for a shell login. I am logging into xfce. how then?

    • Raj Feb 6, 2015 @ 4:19

      Eric, the trap 0, will work whenever you exit a shell. So from any X session like xterm or terminal, the above trap will execute whatever you want it to, when you type exit. Just be careful because it will execute that step, whenever you try to exit the shell from any window. Hope this helps!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.