Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am developing project by using python script in Ubuntu 10.04. In my project,I need to reboot the system automatically at particular time period. So that,I was wrote the below command in my python script.

passwd = "my system password"
os.system('echo %(passwd)s | sudo reboot -f' %locals())

It was working fine but today when I run my script it throws the below error.

sudo no tty present and no askpass program specified
How to resolve this problem.I need to reboot my system through python script.If you know let me, your answer is very useful to me.

share|improve this question
try this… – titusjaka Mar 22 '13 at 11:19

You can restart your computer through dbus, no password required.

(Taken from a previous answer of mine)

#!/usr/bin/env python

import dbus

def main():
    answer = raw_input("Do you really want to shutdown your computer? (Y/n): ")
    if answer in ('y', 'Y', ''):
        bus = dbus.SystemBus()
        bus_object = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer")

if __name__ == "__main__":
share|improve this answer
Thanks and this is another good way to work.But,i need to know how to resolve that tty error now by using existing manner. – Viswa Mar 25 '13 at 8:46

I've found very interesting information that could help your problem.

sudo will only run a program as a different user if one of these 3 conditions has been met (as far as passwords are concerned):

  1. the NOPASSWD option is specified
  2. the user entered the correct target password
  3. the user entered the correct source password

Since options 2 and 3 require a TTY (sudo won't read from a pipe) it won't run if it can't find one. Check your script if at any point you're running a script remotely using ssh, as it's possible that it won't allocate a TTY for a non-interactive remote command.

Source: Post #2 (by pludi) on sudo: no tty present and no askpass program specified (by linuxgeek)

For more information about this solution, see that forum discussion.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.