Tuesday, July 9, 2013

Desktop Friendly Standby, Hibernation, Shutdown and Reboot in Openbox Through Dbus and ConsoleKit

There are quick ways to to shutdown, reboot or put your computer into standby or hibernation in Linux if you have permission to do so. Openbox however does not come with a way of facilitating this and you're stuck with becoming superuser to do so. This is a way to do all this as a normal user.

Basically, we will do what all the desktop environments pretty much also do - we will ask ConsoleKit to handle the machine for us. As normal users, we cannot shutdown the machine ourselves, but we do have permission to instruct ConsoleKit to do it. You most probably won't need to install it, since a lot of desktop programmes on distros that still use dbus depend on it.
This still, for instance, works properly on Debian 7 Wheezy. Bear in mind however that ConsoleKit is not very actively maintained anymore with many distros slowly going over to systemd. If you're using this, then YMMV as to whether and how well this will work. I might tackle that in the future if I ever move to a systemd distro.

We will also need zenity for the dialogue scripts. This is what you'll be seeing as a user:



I have the scripts all assigned to key combos. For instance, CTRL + SUPER (or WIN) + x gives me shutdown, CTRL + SUPER + r is restart, the same but with an h instead would be hibernate and  an s would give me standby.
The combos are basically calling the following scripts that firstly give you an "Are you sure" kind of dialogue, so you can still opt out. Answering No cancels everything, answering Yes commits to running said action. Basically, you paste the scripts from here into a text editor, save it under

 .  

 ~/.bin/  


You then have to make the scripts executable.


1. Shutdown Script

 .  

 #!/bin/bash  
 zenity --question --text="Shutdown?"  
 if [ $? == 0 ]  
 then  
  dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop  
 else  
  exit  
 fi  


WARNING! The long command between then and else must be in one line! Otherwise it won't work. Sorry, but I am yet to devise a better means for showing code samples properly. :( Bear with me!


2. Reboot Script

 .  

 #!/bin/bash  
 zenity --question --text="Restart?"  
 if [ $? == 0 ]  
 then  
  dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart  
 else  
  exit  
 fi  


Again, the dbus command has to be all in 1 line!


3. Suspend

This command will be a bit different, but you can leave out the parts that differ here and that I put out of convenience.
I personally use tint2 as my panel. It had a bug and I'm not sure if that's fixed or not. I keep this workaround just the same. When I resume out of suspend or hibernate, tint2 will show the clock incorrectly at the date I had suspended or hibernated at, that is until it eventually refreshes. Now, if you turned of the machine hours ago, this can be a bit fatal. The command will thus kill all instances of tint2, suspend, and when it resumes, it will start tint2 again.

There is another workaround - resuming from either standby or hibernation will open the desktop without asking for a password. To prevent others from easily using my computer I have told the script to lock the screen right before shutting down.
I have used xtrlock, but am sure that other screen locking or screensaver tools can be used just as well. I prefer xtrlock for its simplicity.

 .  

 #!/bin/bash  
 zenity --question --text="Suspend?"  
 if [ $? == 0 ]  
 then  
  killall tint2 && xtrlock & dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend && tint2  
 else  
  exit  
 fi  


Again, pay attention to the commands between then and else staying in 1 line!
The bold parts are the optional workarounds you can ignore.


4. Hibernate

Quite similar, the only thing that's different is the dbus command:

 .  

 #!/bin/bash  
 zenity --question --text="Hibernate?"  
 if [ $? == 0 ]  
 then  
  killall tint2 && xtrlock & dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate && tint2  
 else  
  exit  
 fi  



That's all.

No comments:

Post a Comment