Run Script on logout/ shutdown ubuntu

8

1

I want to run some commands every time a user of an ubuntu 9.10 machine logs out or shuts down. What is the best way to do this? Every user uses gnome if that helps.

gitnoob

Posted 2010-04-04T23:25:04.043

Reputation:

this should really be under the "Linux & Unix" SE site... IMHO – CenterOrbit – 2011-08-01T03:10:25.917

Answers

3

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.

Arda Xi

Posted 2010-04-04T23:25:04.043

Reputation: 217

/etc/gdm3/ is the slight correction needed – Tim Richardson – 2018-10-16T05:34:12.577

3Using Ubuntu 12.04, /etc/gdm/PostSession/Default doesn't exist, where else I can place my script such that it executes on logout as well as shutdown? – Kushal – 2012-05-12T16:57:43.940

1

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'

mpho raf

Posted 2010-04-04T23:25:04.043

Reputation: 11

1

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*

dericybtes

Posted 2010-04-04T23:25:04.043

Reputation: 11

0

Hmm..well if you google "linux run script on shutdown", the first result is a post on the Ubuntu forums with your answer:

chmod +x /etc/init.d/foo.sh

This will allow you execute the script as a program.

To have a script run at shutdown, use this technique:

sudo ln -s /etc/init.d/foo.sh /etc/rc0.d/K10rensa.sh
sudo ln -s /etc/init.d/foo.sh /etc/rc6.d/K10rensa.sh

background on how this works: An introduction to run-levels.

-

As a sidenote, should I refrain from indulging people with answers when it is clear that they haven't even tried searching the Internet first (I'm new here..)?

Jay Taylor

Posted 2010-04-04T23:25:04.043

Reputation: 183

1This does not handle logout events. Also, welcome to SO! The only time failing to search becomes an issue is when someone is asking a question that has clearly been asked (here) before. Googling prior is not required. As the link you provided clearly doesn't fully answer the question, its a good chance that the OP already saw it :) – Tim Post – 2010-04-04T23:49:54.323

0

You can probably use the ConsoleKit dbus interface.

baol

Posted 2010-04-04T23:25:04.043

Reputation: