LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Gnome - run script on logout (https://www.linuxquestions.org/questions/linux-desktop-74/gnome-run-script-on-logout-724453/)

arizonagroovejet 05-07-2009 01:26 PM

Gnome - run script on logout
 
I'm investigating the migration of a bunch of machines from KDE to GNOME.

I currently have a collection of scripts which run when a user logs out. KDE provides an easy way to run these scripts - stick them in /opt/kde3/shutdown (or equivalent depending on distro). Any scripts in there get run as the user that's logged out.

The only suggestion I can find for doing this in GNOME is to put scripts in to /etc/gdm/PostSession. The trouble with that is that the scripts then run as root. Some of the scripts I need to run manipulate stuff in a the user's home directory which is mounted from a remote server and of course root doesn't have the ability to mess with stuff in such home directories. So a script which runs as root is at best annoying and at worse useless for my purposes.
I might be able to do something in the script like

Code:

su - $USER -c "some commands"
but that seems kinda messy and I haven't tested it yet.


Is there any sensible, easy method to make GNOME run a script when a user logs out with the permissions of that user?

penguiniator 05-08-2009 10:08 PM

You could do something like this (from http://ubuntuforums.org/showthread.php?t=252935) in /etc/gdm/PostSession/Default:

logoutscript="$HOME/.gdmlogout";
if [ -x "$logoutscript" ] ; then
su $USER -c "$logoutscript"
fi

which is very similar to what you are thinking. This has the advantage of letting users define their own actions as well.

arizonagroovejet 05-09-2009 05:06 AM

I've tried running a script under su and it does work as expected so looks like I'll be using that method.

Giving the user the ability to define the logout script is not what I was after but maybe I'll do that as well as the scripts they can't define.

It seems like a rather large omission that GNOME doesn't provide a neater method of defining logout scripts.

cheers.

seamusph 06-02-2009 08:14 AM

I've had a similar problem. I needed to run a command before gnome exits (and not after). I wrote a small python script to listen to the gnome 'save-yourself' event' and then run my command.

Hopefully somebody might find this useful.

Code:

#!/usr/bin/env python

#Author: Seamus Phelan

#This program runs a custom command/script just before gnome shuts
#down.  This is done the same way that gedit does it (listening for
#the 'save-yourself' event).  This is different to placing scipts
#in /etc/rc#.d/ as the script will be run before gnome exits.
#If the custom script/command fails with a non-zero return code, a
#popup dialog box will appear offering the chance to cancel logout
#
#Usage: 1 - change the command in the 'subprocess.call' in
#          function 'session_save_yourself' below to be what ever
#          you want to run at logout.
#      2 - Run this program at every gnome login (add via menu System
#          -> Preferences -> Session)
#
#

import sys
import subprocess
import datetime

import gnome
import gnome.ui
import gtk


class Namespace: pass
ns = Namespace()
ns.dialog = None


def main():
    prog = gnome.init ("gnome_save_yourself", "1.0", gnome.libgnome_module_info_get(), sys.argv, [])
    client = gnome.ui.master_client()
    #set up call back for when 'logout'/'Shutdown' button pressed
    client.connect("save-yourself", session_save_yourself)
    client.connect("shutdown-cancelled", shutdown_cancelled)


def session_save_yourself( *args):
    #Lets try to unmount all truecrypt volumes
    retcode = subprocess.call("truecrypt -d", shell=True)
    if retcode != 0:
        #command failed 
        show_error_dialog()
    return True

def shutdown_cancelled( *args):
    if ns.dialog != None:
        ns.dialog.destroy()
    return True


def show_error_dialog():
    ns.dialog = gtk.Dialog("There was a problem running your pre-shutdown script",
                          None,
                          gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                          ("There was a problem running your pre-shutdown script - continue logout", gtk.RESPONSE_ACCEPT))
    if ns.test_mode == True:
        response = ns.dialog.run()
        ns.dialog.destroy()
    else:
        #when in shutdown mode gnome will only allow you to open a window using master_client().save_any_dialog()
        #It also adds the 'Cancel logout' button
        gnome.ui.master_client().save_any_dialog(ns.dialog)



#Find out if we are in test mode???
if len(sys.argv) >=2 and sys.argv[1] == "test":
    ns.test_mode = True
else:
    ns.test_mode = False

if ns.test_mode == True:
    main()
    session_save_yourself()
else:
    main()
    gtk.main()

cheers

arizonagroovejet 12-09-2009 07:06 AM

Well I finally got around to trying that Python script. Whilst it does exactly what it says and is very cunning, like the GDM PostSession script is doesn't get called if the user ends the GNOME session by way of rebooting or shuting down the machine. It has the advantage over using GDM scripts that you can run something as the user that's logging out without having to use su though. What I really need now is some way getting a script to run if the user exits GNOME by way or restart or shut down.


All times are GMT -5. The time now is 09:13 AM.