The main problem with adding a shutdown/restart buton to the desktop is that you have to use a sudo command in the launcher that you are using. Because of this, you have to supply a password, which defeats the purpose of a 1-click button to shutdown. Using /etc/sudoers and a pyGTK script, we can defeat this and create a true shutdown method.
Info:
Tested on (U,K)buntu and Debian (Stable/Testing/Unstable)
BUT - should work on any Linux distro utilizing these packages and sudo.
You need GTK+ libraries, and python2.X. Also may work on previous versions of Python.
Step 1:
You need to access /etc/sudoers
You can do this by using the visudo command. You need to do this using sudo or root terminal. Visudo will alert you if there are errors with your sudoers file after you have modified it.
Code:
visudo -f /etc/sudoers
Under "# Cmnd alias specification", add the following:
Code:
Cmnd_Alias SHUTDOWN = /sbin/shutdown
Cmnd_Alias HALT = /sbin/halt
Cmnd_Alias REBOOT = /sbin/reboot
or
NOTE: Some users may need to use the following instead:
Code:
Cmnd_Alias SHUTDOWN = /usr/sbin/shutdown
Cmnd_Alias HALT = /usr/sbin/halt
Cmnd_Alias REBOOT = /usr/sbin/reboot
This will create the aliases SHUTDOWN, HALT, and REBOOT that we will need for specifying user privelages.
Under "# User privelage specification", add the following:
Code:
<user> ALL=NOPASSWD: SHUTDOWN, HALT, REBOOT
<user> = the user that needs the shutdown button on the desktop. This let's the machine know that when <user> uses sudo to shutdown, halt, or reboot, they will not have to specify a password.
"ALL" specifies that this works on all the machines. You can put your machine name here if you wish to do so, but it probably makes no difference.
Step 2:
We can create a Launcher on the desktop that shuts the system down when it is clicked, but what if you click it by accident? That would suck. To fix this, I made a python script that opens a GTK interface that lets you select what you would like to do - shutdown, restart, or cancel the action.
Use the following python script code and save it as the file "power_options". Place it somewhere safe and where you can access it.
Code:
#!/usr/bin/env python
import os
import sys
import pygtk
import gtk
class power:
def restart(self, event):
command = "sudo shutdown -r now"
os.system(command)
def shutdown(self, event):
command = "sudo shutdown -h now"
os.system(command)
def cancel(self, event):
import sys
sys.exit()
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Power Options")
self.window.connect("delete_event", self.delete_event)
self.window.set_border_width(10)
self.box1 = gtk.HBox(False, 0)
self.window.add(self.box1)
self.button1 = gtk.Button("Restart")
self.button1.connect("clicked", self.restart)
self.box1.pack_start(self.button1, True, True, 0)
self.button2 = gtk.Button("Shutdown")
self.button2.connect("clicked", self.shutdown)
self.box1.pack_start(self.button2, True, True, 0)
self.button3 = gtk.Button("Cancel")
self.button3.connect("clicked", self.cancel)
self.box1.pack_start(self.button3, True, True, 0)
self.window.set_position(gtk.WIN_POS_CENTER)
self.button1.show()
self.button2.show()
self.button3.show()
self.box1.show()
self.window.show()
def main():
gtk.main()
if __name__=="__main__":
pwr = power()
main()
Step 3:
Now we need to make a Launcher on the desktop that initializes the python script. For the "Command" entry, add:
Code:
python <location of script here>/power_options
Choose the "Application" option for the Launcher. If you choose to execute the command in a terminal, you will get a terminal box everytime you click the desktop Launcher.
Choose a nifty icon for your new launcher, and click OK.
Double-click your new Launcher, and you should get a pop-up window asking if you would like to shutdown, reboot, or cancel the command.
Enjoy!
Bookmarks