am using debian lenny and tried to create a shutdwon script using python
import os
os.system("sudo shutdown -h now")
i cant use this in debian lenny then how will i implement
am using debian lenny and tried to create a shutdwon script using python
import os
os.system("sudo shutdown -h now")
i cant use this in debian lenny then how will i implement
Your GUI app is failing because sudo expects a terminal to prompt for a password. From a GUI there is no TTY, so os.system("sudo ...") quietly does nothing. is right: keep root access tight. Two clean approaches on Debian Lenny are below.
shutdown directly and works well in desktop sessions.import dbus
bus = dbus.SystemBus()
ck = bus.get_object('org.freedesktop.ConsoleKit',
'/org/freedesktop/ConsoleKit/Manager')
iface = dbus.Interface(ck, 'org.freedesktop.ConsoleKit.Manager')
iface.Stop() # requests system shutdown; polkit handles auth Notes:
Stop() and Restart() for this exact use case. See the ConsoleKit Manager API. ConsoleKit Manager APIshutdown, but supply a GUI askpass helper to sudo as @Furdo hinted.Create ~/bin/askpass.sh:
#!/bin/sh
zenity --password --title="Authentication" Then call it from Python:
import os, subprocess
env = os.environ.copy()
env['SUDO_ASKPASS'] = os.path.expanduser('~/bin/askpass.sh')
subprocess.check_call(['sudo', '-A', '/sbin/shutdown', '-h', 'now'], env=env) The -A flag tells sudo to use the askpass helper; SUDO_ASKPASS points to your script. Details are in the sudo manual. Sudo manual
Extras:
gksu/gksudo exists and will pop up a password dialog for /sbin/shutdown. gksu package (includes Lenny)visudo to grant only your user NOPASSWD for /sbin/shutdown (and/or /sbin/reboot). Be precise with paths and avoid broad rules.Jump to Post— woooee 814i cant use this in debian lenny
I don't understand why you can't use this. Also, you probably only want root access for the script, otherwise anyone can shut down your system. If you want to run it from a non-root user, then you or the program will also have …
Jump to Post— SoulMazer 26Could you be more descriptive than "I can't use this"? Do you get an error? Does it just not do anything in general?
As you probably know, you need elevated privileges to run a command with "sudo," so my guess is that you need to run your script with …
i cant use this in debian lenny
I don't understand why you can't use this. Also, you probably only want root access for the script, otherwise anyone can shut down your system. If you want to run it from a non-root user, then you or the program will also have to be able to supply the correct password, which also is a bad idea.
Could you be more descriptive than "I can't use this"? Do you get an error? Does it just not do anything in general?
As you probably know, you need elevated privileges to run a command with "sudo," so my guess is that you need to run your script with "sudo": sudo python MyScript.py
this is working if am going through terminal but here am using a gui so after running the script its not promting for password and its not getting shutdown
choice=='Shutdown':
msgbox("You chose to Shutdown")
msg = "Do you want to schedule your Shutdown"
title = "Confirmation"
choices=
choice=buttonbox(msg,title,choices)
if choice=='Yes':
t=integerbox('Enter the time in minutes :')
tim=t*60
time.sleep(tim)
#os.system('sudo shutdown -h now')
commands.getoutput('sudo shutdown -r now')
else:
os.system('sudo shutdown -h now')
You can or at least used to be able to shutdown in python using python dbus Hal.
The following works in Slackware 13 Mandriva and Fedora.
import dbus
bus = dbus.SystemBus()
shut = bus.get_object('org.freedesktop.Hal',
'/org/freedesktop/Hal/devices/computer')
shutr = dbus.Interface(shut , 'org.freedesktop.Hal.Device.SystemPowerManagement')
shutr.Shutdown() But dbus is a can of worms and constantly changing giving us in linux all the joys of the Windows registry. So in ubuntu 10.10 hal has disappeared and UPower has appeared at least in the default installation. I can no longer shutdown but with UPower I can Hibernate or Suspend. The following works in Ubuntu 10.10 for me (of course some hardware setups have problems with hibernate).
import dbus
bus = dbus.SystemBus()
shut = bus.get_object('org.freedesktop.UPower',
'/org/freedesktop/UPower')
shutr = dbus.Interface(shut , 'org.freedesktop.UPower')
shutr.Hibernate() To just use halt or shutdown explore sudo -A and SUDO_ASKPASS
with say a simple little pygtk or pyqt dialog to get the password.
I already posted this link. It may be worth reading http://www.spencerstirling.com/computergeek/shutdown.html
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.