One of the bigger omissions from GNOME is that there is no built-in mechanism for executing commands at logout. I discovered this the hard way when trying to set up MPD to run completely in userspace (to be documented in an upcoming post). However, it is possible to make GDM run arbitrary stuff at the end of the session, and that can be used to good effect as described here. In the event that that link goes bad, in summary what you do is edit /etc/gdm/PostSession/Default
to the file. Then create a file .gdmlogout in your home directory and make it executable.
logoutscript="$HOME/.gdmlogout"; if [ -x "$logoutscript" ] ; then su $USER -c "$logoutscript" fi
You need to note that this will only work if you are using GDM and the .gdmlogout file will execute no matter what desktop environment you log into.