Friday, May 4, 2012

Quit Session Button / Command for Linux

I recently installed the new, final version of Ubuntu 12.04 a.k.a Precise Pangolin, and found it a very good Ubuntu release. It is more stable, faster, and much more responsive than its previous version.

I think its shiny Unity interface is the best user interface mainly for new users. But for me, an Ubuntu user who like experimenting with operating system installation, Unity did not last very long on my PC. I'm not saying that I a Linux expert, no, I am still a noob in Linux world, but I just like to do some experiment to installed system. I like to change its interface, by installing some other desktop, thanks to availibility of desktop environments in Ubuntu/Linux. That is one of the reasons why I like Ubuntu over any other Linux distro. If a software is made available for Linux, then it is almost sure that there is an EASY installation or version for Ubuntu.

I tried installing some different desktop envioronments including its official derivates based on KDE (Kubuntu), XFCE (Xubuntu) and LXDE (Lubuntu), other desktop environment including MATE desktop, the fork of oldies but goodies GNOME 2, and also with combination including standalone session using Compiz.

Basically, there is no problem I found when using standalone Compiz session, except a little problem that I could not find an easy button or a way for logging out smoothly from the Compis session. So, I started searching some information on the internet for the solution. I didn't find many, but I did find enough working script.

So, by editing and combining command and script I found, I finally able to made a simple script that works. The screenshot below is the Quit Session window that I made, based on scripts I found on Arch Linux and Ubuntu forums.

And below is the code, a python script, I gave this tiny app a name: "Quit Now!", an agnostic Linux desktop session utility, for immediately quitting current session (Log out, Reboot and Shutdown):

#!/usr/bin/env python

import pygtk
import gtk
import os

class DoTheLogOut:

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

    def onkeypress(self, widget, event):
        if event.keyval == gtk.keysyms.Escape:
            return False

    def lost_focus(self, widget, event):
        if self.focus_check:
            return False

    # Logout
    def logout(self, widget):
        os.system("pkill -TERM -u $(whoami)")

    # Reboot
    def reboot(self, widget):
        os.system("dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart")

    # Shutdown
    def shutdown(self, widget):
        os.system("dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop")

    def __init__(self):
        # Main window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Quit Session")
        self.window.connect("delete_event", self.delete_event)

        # Box container
        self.box1 = gtk.HBox(False, 5)

        # Cancel button
        self.button1 = gtk.Button("_Cancel")
        self.button1.connect("clicked", self.delete_event, "Canceled.")
        self.box1.pack_start(self.button1, True, True, 0)

        # Spacer
        self.emptytext1 = gtk.Label(None)
        self.box1.pack_start(self.emptytext1, True, True, 0)

        # Logout button
        self.button2 = gtk.Button("_Log out")
        self.button2.connect("clicked", self.logout)
        self.box1.pack_start(self.button2, True, True, 0)

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

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

        self.focus_check = True
        self.window.connect("focus-out-event", self.lost_focus)
        self.window.connect("key-press-event", self.onkeypress)

def main():

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

Like you can see on the script above, I use following command for each quit action option:

  • Logout:
    pkill -TERM -u $(whoami)
  • Reboot/Restart:
    dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart
  • Shutdown/Power Off:
    dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop

I found this very useful in somecase ultimately with custom desktop session like Compiz standalone session, and it works on any desktop environmet (I believe :))

So, if you are interested to use it, just copy the code above and paste on an empty text file, and than save it. To use it, simply double-click the file.

You can also download the file, I already upload it, ready to use. Download here:

Hope you find this useful. Thank you.


PS: Still, sorry for my English :)
Google+ Comments Disqus Comments
Disqus Comments Google+ Comments
comments powered by Disqus