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

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.

Edited 6 Years Ago by woooee: n/a

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.

This article has been dead for over six months. Start a new discussion instead.