LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 05-07-2009, 01:26 PM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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?
 
Old 05-08-2009, 10:08 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
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.
 
Old 05-09-2009, 05:06 AM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
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.
 
Old 06-02-2009, 08:14 AM   #4
seamusph
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
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
 
Old 12-09-2009, 07:06 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Script on GDM Logout? '58LesPaul Linux - Desktop 4 04-11-2008 09:13 PM
I need a script to run at user logout jr_linux Linux - Newbie 3 03-25-2008 02:41 PM
loginscript--run script-- logout sinister1 Linux - Security 6 01-16-2008 10:44 AM
would script run after logout? mokku Linux - Newbie 3 12-03-2007 11:21 AM
On gnome / x logout run "logout" ?? once here Linux - Software 1 01-09-2004 02:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration