Tuesday, April 1st, 2008

Openbox Logout, Reboot & Shutdown Script

I have been using the latest Openbox release [3.4.7-pre2] for the last few weeks and so far I have been impressed. One of the new features from the latest release is a GDM control script. The script basically allows for a user to send reboot and shutdown signals to GDM from within the Openbox environment. This provides a means to reboot/shutdown an Openbox system in a clean and efficient manner.

Screenshot of Openbox logout, reboot, shutdown script.

I have written the following PyGTK script to take advantage of the new GDM control. Python is not currently a language that I am too familiar with, so please feel free to rip the script to bits improve as you see fit.

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import os

class DoTheLogOut:

    # Cancel/exit
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    # Logout
    def logout(self, widget):
        os.system("openbox --exit")

    # Reboot
    def reboot(self, widget):
        os.system("gdm-control --reboot && openbox --exit")

    # Shutdown
    def shutdown(self, widget):
        os.system("gdm-control --shutdown && openbox --exit")

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Exit? Choose an option:")
        self.window.set_resizable(False)
        self.window.set_position(1)
        self.window.connect("delete_event", self.delete_event)
        self.window.set_border_width(20)

        # Create a box to pack widgets into
        self.box1 = gtk.HBox(False, 0)
        self.window.add(self.box1)

        # Create cancel button
        self.button1 = gtk.Button("Cancel")
        self.button1.set_border_width(10)
        self.button1.connect("clicked", self.delete_event, "Changed me mind :)")
        self.box1.pack_start(self.button1, True, True, 0)
        self.button1.show()

        # Create logout button
        self.button2 = gtk.Button("Log out")
        self.button2.set_border_width(10)
        self.button2.connect("clicked", self.logout)
        self.box1.pack_start(self.button2, True, True, 0)
        self.button2.show()

        # Create reboot button
        self.button3 = gtk.Button("Reboot")
        self.button3.set_border_width(10)
        self.button3.connect("clicked", self.reboot)
        self.box1.pack_start(self.button3, True, True, 0)
        self.button3.show()

        # Create shutdown button
        self.button4 = gtk.Button("Shutdown")
        self.button4.set_border_width(10)
        self.button4.connect("clicked", self.shutdown)
        self.box1.pack_start(self.button4, True, True, 0)
        self.button4.show()

        self.box1.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    gogogo = DoTheLogOut()
    main()

12 Responses to “Openbox Logout, Reboot & Shutdown Script”

  1. uterrorista wrote,

    Hi! Looks god to me. But, how do i run the script?? :D

  2. Philip wrote,

    Hi uterrorista
    To create/run the script, copy it into a new file say, /usr/bin/openbox-logout and make the file executable. You can then call the script from your Openbox menu or via the terminal with openbox-logout — or whatever you named the file.

    I should also have mentioned that the GDM features are available in Openbox 3.4.7-pre2.

  3. j wrote,

    You create a file named whateveryouwant.py and then locate the file with the terminal and cd and chmod it with +x (chmod +x whaterveryounamedyoufile.py). And then just use it as a program. For example just link to it from your openbox menu and it will run!

    To create a "real" program of it you can put it in the bin/ folder and take away the file ending. Test it by typing the filename!

  4. fauno wrote,

    GDM for Gnome D… Manager? (Display??) what about something lighter, like SLiM?

    btw, nice script!

  5. uterrorista wrote,

    Great! Thanks! It's working. I'm gonna add this to my "Openbox introduction" post!

    I'll leave the credits for you! ;)

  6. uterrorista wrote,

    i can only make logout. I do not have gdm-control installed.

    I'm using ubuntu and i can't find it. How do i install gdm-control?

  7. Philip wrote,

    Hi uterrorista,

    You will need to have the latest version of Openbox installed, version 3.4.7-pre2. It is a pre-release so use at your own risk. Having said that, I have been using it for several weeks and have not had any problems.

    You can check which version of Openbox you are running by entering the following terminal command:

    openbox --version
    

    You can get the latest version of Openbox from my repository, see:

    1. Repository howto: http://www.crunchbang.org/wiki/CrunchBangLinuxRepository
    2. Repository Packages: http://www.crunchbang.org/wiki/CrunchBangLinuxRepositoryPackages

    Again, use at your own risk ;)

  8. uterrorista wrote,

    Thanks again! Nice repo there. Cool applications!

  9. MTecknology wrote,

    Very impressed. I haven't tried it yet but I did run it. I added my own button for Suspend and will try to make another one for Hibernate and then try to figure out how to split these to two rows.

    The only other thing I would like from this - is shortcut keys. So I can do something like Alt+S to shutdown.

    By the way, Thanks very much.

  10. emanuele wrote,

    Wow!!! this was amazing, working as well on debian sid!!!!

    I definitely love this space, it's pkenty of clever stuff!!!

  11. VCoolio wrote,

    Hi,

    here is a modified version of your script; I added icons on the buttons. Code: http://pastebin.com/f10ee61ac Preview: http://img149.imageshack.us/img149/567/screenshotexit.png

    thx for the script, very useful.

  12. Philip wrote,

    @VCoolio: Nice one, thank you. :)

Add Your Comment

Use the form below to add your comment. Markdown syntax is available. Note, comments are moderated by me for spam filtering. Alternatively, feel free to contact me privately.