9

I am in my university labs running ubuntu 12.10 with unity and I don't have root privilleges. I need to run a script at logout. Is this possible?

Note: this is probably a duplicate of this question, however the answers given are quite cryptic and no specific directions are given.

geo909
  • 202
  • 3
  • 8
  • Depends on whether it's a graphical login, or "command-line" login – geirha Jul 07 '13 at 18:45
  • It's a graphical login. – geo909 Jul 07 '13 at 18:46
  • 3
    I guess You can write a log-out script which runs what You want, and then logs out with gnome-session-quit or something like that. – Adobe Jul 07 '13 at 19:47
  • @adobe Hmm.. Thanks, that sounds like a good workaround in my case. It's weird though that a non-privilleged user does not seem to have a straightforward way to deal with this situation.. Btw I do not seem to be able to vote up, probably due to low rep. – geo909 Jul 07 '13 at 22:42
  • @geo909: why study yet another system -- when a simple bash script solves it? Let me know if You'll be in trobles with bash script. – Adobe Jul 08 '13 at 08:05
  • @adobe It's a good solution and I'll very happily go with it. Just not the most elegant :) I have some scripts that I not only use when logout so I'll have to have two versions, one that logs out and one which is not.. So I'll have to also have different shortcuts or keyboard bindings for those. Plus my force of habit is to log out using the button on the upper bar; I cannot add a new button or something to use my script, I cannot configure anythig in the labs.. – geo909 Jul 08 '13 at 15:21
  • No need for two scripts: make one script which takes a parameter. No need for two buttons: for logout make the same button with shift key. I think it is possible to bind to upper bar also. – Adobe Jul 08 '13 at 15:39
  • 1

1 Answers1

5

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()

  1. Make it executable:

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

     #!/usr/bin/bash
     touch ~/Desktop/AAAAAAAAAAAAAAAAAAAAAAAAAAA  
    
  3. 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).

totti
  • 6,818
  • 4
  • 39
  • 48