Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

It works fine from terminal, but when called from a thread in python, it fails.

history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00

from python, here's what I did:

halt = "history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00"
thread.start_new_thread(lambda: commands.getoutput(halt),())

I used lambda after trying it for other commands like opening nautilus or gedit, which works fine, but shutdown command isn't!

share|improve this question

2 Answers 2

up vote 3 down vote accepted

This:

history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00

is not actually a command, it's a fragment of shell script. So you need to have the shell execute it. Something like this:

import os
halt = "history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00"
os.system(halt)
share|improve this answer

From man sudo:

The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character.

So, after the -S, it's waiting for a NEWLINE character which you provided when using the terminal.

share|improve this answer
    
As the OP said, that command works fine from terminal and indeed, echo without -n argument will output the trailing newline. –  Radu Rădeanu Oct 2 '13 at 21:53

Your Answer

 
discard

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.