1

Hi guys and gal's I have a problem.

I'm executing a python script that needs to run a sudo command to continue. This is the command:

sudo /etc/init.d/test restart

The issue is that no matter how I execute this it only runs sudo and /etc/init.d/test and returns:

sudo: /etc/init.d/test: command not found

The issue seems to be that restart is not sent along with the command.

These are the ways I've tried running the command:

Attempt 1 using os

os.system('sudo /etc/init.d/test restart')

Attempt 2 using subprocess

x = subprocess.Popen(['sudo','/etc/init.d/test','restart'])
x.communicate()

Attempt 3 using subprocess again

x = subprocess.Popen(['sudo','/etc/init.d/test restart'])
x.communicate()

This actually returned:

sudo: /etc/init.d/test restart: command not found

Which doesn't make sense since if I execute the command directly on the system it works.
Any ideas as to how I could do this?

4
  • 1
    Duplicate of stackoverflow.com/questions/567542/… ? Commented Apr 3, 2013 at 12:50
  • Pretty much but the answer given there did not work for me. As you can see the full path is given and it still does not work :( Should I have asked the question again in that thread? If so I'm sorry I thought since it did not answer me I could just ask again. Commented Apr 3, 2013 at 13:11
  • The sudo command will give you root privileges, and needs your password to do so. It's a really, really bad idea to give root access without a password Commented Apr 3, 2013 at 13:14
  • This will work on a bunch of machines with different passwords so I figured it would be best if I just left it to the user to input the password. Commented Apr 3, 2013 at 13:18

2 Answers 2

3
#!/usr/bin/env python
import subprocess,getpass
password = getpass.getpass()
#proc = subprocess.Popen(
#  ['sudo','-p','','-S','/etc/init.d/test','restart'],
#   stdin=subprocess.PIPE)
proc = subprocess.Popen(
    ['sudo','-p','','-S','echo','restart'],
    stdin=subprocess.PIPE)
proc.stdin.write(password+'\n')
proc.stdin.close()
proc.wait()
Sign up to request clarification or add additional context in comments.

2 Comments

When I execute this nothing happens and after a bit the script continues. Now since this should restart the system I'm not sure whether it worked or not. Is it suppose to be just a blank screen or could there be an error?
The script just prompts for a password and prints the word 'restart'. I tried it and it works for me.
0

If you get this:

sudo: /etc/init.d/test: command not found

it indicates that /etc/init.d/test doesn't exist. Try it yourself like this from the command line:

> sudo blarg whatever whatever
sudo: blarg: command not found

So just make sure that the command you are trying to execute really exists:

ls -l /etc/init.d/test

If you still have problems, then try to simplify things by first just getting it to execute 'sudo whoami' -- once you get that working, change it to your "real" command.

1 Comment

Yeah but see I only get that because it doesn't run the full command /etc/init.d/test restart. If I were to run the full command in the linux console it works. Thanks for the help though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.