Execute script before shutting down
You could add your command to the file /etc/gdm/PostSession/Default
before the exit 0
line.
You can do this by creating a script in /etc/rc0.d (for shutdown) and /etc/rc6.d (for reboot). To do this, run:
gksu gedit /etc/rc0.d/K01mycustomscript
Edit your shutdown script in the text editor then run:
sudo chmod +x /etc/rc0.d/K01mycustomscript
And if you want it to run on reboot also then run this:
sudo cp /etc/rc0.d/K01mycustomscript /etc/rc6.d/K01mycustomscript
Source: this forum thread
Sounds like a question for superuser.com to me. Anyway, after some Googling I found How to run a script on logout? which says to add the script to $HOME/.bash_logout.
If that doesn't work, add
$HOME/.bash_logout
to /etc/gdm/PostSession/Default so it executes the logout script properly.
Seeing as before shutdown the user is logged off anyway, this should cover both bases.