Run Script on logout/ shutdown ubuntu

Solution 1:

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.

Solution 2:

If you are using KDE (I am using Ubuntu with kdm)

# From the kdm man page
#  http://manpages.ubuntu.com/manpages/oneiric/man1/kdm.1.html

/etc/kde4/kdm/Xreset - script to run as root after session exits

/etc/kde4/kdm/Xsession - script to run as user after login of user

/etc/kde4/kdm/Xsetup - script to run as root before the login dialog appears

/etc/kde4/kdm/Xstartup -  script to run as root before session starts

To test:

Open the appropriate file i.e.

sudo emacs /etc/kde4/kdm/Xreset

Add a test command (create a file in my home directory)

touch  /home/dericbytes/this-file-was-created-by-xreset

Then I logged out, and back in again, and checked if the new file was created

ls /home/dericbytes/this*

Solution 3:

If you don't want to be bound to a specific display manager then you can put your scripts under /etc/X11/Xreset.d/ which will be run as root every time you logout from an xsession.

For example: I want my virtualbox VM's to be on the savestate everytime a user logs out so I have a script that does that. #!/bin/bash su - $USER -c 'vboxmanage list runningvms | cut -d\" -f2 | xargs -I{} vboxmanage controlvm {} savestate'