Firefox not closed properly when loging out

Apparently there is no dedicated file to write to like there used to be. It was the /etc/gdm/PostSession under Gnome but isn't available since apparently Unity. And placing a script under /etc/rc0.d/ and /etc/rc6.d/ will not work since they are only executed after Xserver is shut down.

However there is an event called gnome-save-yourself fired when gnome has been demanded to log out. I is what gedit for instance will implicitly call. And Seamus Phelan made a python script to listen to the event and run your script when caught.

Here's the link to the solution : Script Execution at user logout (non root user)

Follow instructions and then all you have to do is place your script that may look like this:

#! /bin/sh
#Requires wmctrl
wmctrl -c firefox;
gmusicbrowser -cmd "Quit";
exit 0;

where the option -c gently closes Firefox as does -cmd "Quit" for gMusicBroswer. Both Firefox and gMusicBrowser need to be closed properly which they do not.

A probably better way would be to check the use of SIGTERM, SIGKILL and kill timeout in Ubuntu and the programs but I don't know enough about it.