Ubuntu – Executing a logout script

bashcommand linelogoutscripts

I've been teaching myself how to write login scripts, but I am having difficulty finding information about writing a LOGOUT script. I want to be able to record a log entry for when a user logs out of the X session.

Here's what I want to execute upon LOGOUT:

#!/bin/bash

# This script is intended to record when a user logs out of a TTY shell or the X session.
# This will record the results to a log file: logoutScript.log

# Variables used in this script.
dDate=$(date +%Y/%m/%d)
dTime=$(date +%l:%M%P)
logDir=/opt/scripts/logs
logFile=$logDir/logoutUser.log

# Begin operation of script.
echo "$dDate, $dTime - User '$USER' has logged out of the system." >> $logFile

# End of file
exit 0

I know that if I use the following on a login script, it works the way I want it. So how do I do the reverse for a LOGOUT script?

# Detect if user is logged into TTY shell or X session, then execute corresponding login script.
if xhost >& /dev/null ; then
    gnome-terminal -e "bash -c \"cd /opt/scripts && ./loginScripts.sh && cd $HOME \""
else 
    bash -c "cd /opt/scripts && ./loginScripts.sh && cd $HOME"
fi

I would really appreciate any help. Thanks.

Best Answer

Open terminal (or also known as command line) by pressing together Ctrl+Alt+T or by finding it in the dash.

Run this command sudo gedit /etc/lightdm/lightdm.conf. This opens text editor with lightdm.conf file located in /etc/lightdm folder.

In the file you should have a line [SeatDefaults]. Below this line, enter session-cleanup-script=/path/to/your-script.sh , where /path/to/your-script.sh is the actual address of the script that you want to run.

Note: make sure your script is set to executable by running:

sudo chmod +x /path/to/script.sh

Save and exit.

NOTE: as of 14.04 version, the config file doesn't exist and must be created manually OR copied from an example file using the following command:

sudo sh -c 'zcat /usr/share/doc/lightdm/lightdm.conf.gz > /etc/lightdm/lightdm.conf'

If you just want a script on a logout of lime a terminal, just put that in ~/.bash_logout.