Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to run a script before I log out of my GNOME session, to warn myself if I have forgotten a pendrive plugged into the machine.

However, all solutions I have found have not been enough for me:

  • This similar question suggests editing the /etc/gdm/PostSession/Default file, but I don't have the permissions to do that. (And the file is currently empty on my machine, so there are no existing hooks to take advantage off)

  • I also found this post suggesting a workaround but, according to the first reply, it does not work if I shutdown the computer instead of only logging off.

share|improve this question
    
Related, for Unity: Script Execution at user logout (non root user) –  Gilles Jan 11 at 1:23
add comment

3 Answers

As you are using Gnome, you can use the Python script below, adapted from the one you mentioned.

It needs a Gnome logout (ie gnome-session-quit) (or gnome shutdown), the one happens when we use the logout in GUI. AFAIK no process can block a shutdown by the command sudo shutdown -h 0 or by sudo poweroff. When shutdown is execued it gives SIGTERM to all process and gives a few seconds to them to exit (After executing some scripts which can't be edited by non root users ). If not exited processes are forcefully killed by SIGKILL.

This is the step by step procedure of gnome_save_yourself method. Let's do a test.

  1. Save following code as ~/Desktop/execute_script_on_shutdown.sh (From http://www.linuxquestions.org/questions/linux-desktop-74/gnome-run-script-on-logout-724453/#post3560301)

    #!/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
    
    
        #execute shutdowwn script
        #########################################################################################
        retcode = subprocess.call("bash /home/totti/Desktop/shutdown_script.sh", 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() 
    
  2. Make it executable:

    chmod +x ~/Desktop/execute_script_on_shutdown.sh
    
  3. Save the following as ~/Desktop/shutdown_script.sh

    #!/usr/bin/bash
    touch ~/Desktop/AAAAAAAAAAAAAAAAAAAAAAAAAAA  
    
  4. Execute the main script

    bash ~/Desktop/execute_script_on_shutdown.sh
    

Now you feel the script wait for something

  1. Log Out or shutdown your OS (Ubuntu)
  2. Log in
  3. Check for a file named AAAAAAAAAAAAAAAAAAAAAAAAAAA on your desktop.

    ls -l ~/Desktop/AAAAAAAAAAAAAAAAAAAAAAAAAAA
    

If you see the file everything OK. Now you can edit the shutdown_script.sh to suit your need. Also remember to execute the execute_script_on_shutdown.sh on login (or make it auto executable on startup).

share|improve this answer
add comment

If you want your session to block under all circumstances you need root-privileges. There's no way around that. User root can always kill -9 your processes. I am surprised that a shutdown does not make gnome emit the "save-yourself" signal. Also I believe the "PostSession" scripts are only run after gnome-session has terminated and (I believe) just before the Xserver terminates, meaning that's not the place where you would want to put a warning that's supposed to show up on screen (if I'm correct).

What may work is a Gnome application that a) reacts on the "save-yourself" gnome event AND b) reacts on SIGTERM in the same way it reacts to "safe-yourself". Beyond that there's little you can do, especially without root privileges.

However you can solve the non-root problem: Write a PostSession script that does what you want and suggest to somebody with root-privileges to deploy it on all machines as it is a sensible tool that helps users a lot. Usually the guys with root-privileges are paid to make/keep users happy. :-)

What's the problem you are trying to solve? Why may you not logout of your session when a pendrive is plugged in?

You can have a dbus client that displays "Don't forget to unplug the device!" when gvfs announces unmounting a Filesystem on a USB device. But I don't know how well that works or even serves your purposes.

share|improve this answer
    
This is for my account at the computer lab; If I forget my pendrive plugged in when I logout I can leave it behing when I go home. This is also why I don't have root priviledges (and unfortunatly asking anything for the admins here is a very bureocratic process). What would I have to do to make a dbus client like you said? I never worked with something like this and I can't find any documentationon telling what messages gcfs supports when I search for it. –  missingno Sep 28 '12 at 14:19
add comment
up vote 0 down vote accepted

I finally managed to actually test the Python script I mentioned as the second option in my question. It turns out that it does work when asking to shut down as well, not just on reboot.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.