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?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Adilicious
  • 1,643
  • 4
  • 18
  • 22
  • 1
    Duplicate of http://stackoverflow.com/questions/567542/running-a-command-as-a-super-user-from-a-python-script ? – Paul Schreiber Apr 03 '13 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. – Adilicious Apr 03 '13 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 – Maykel Llanes Garcia Apr 03 '13 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. – Adilicious Apr 03 '13 at 13:18

2 Answers2

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()
Hal Canary
  • 2,154
  • 17
  • 17
  • 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? – Adilicious Apr 03 '13 at 13:49
  • The script just prompts for a password and prints the word 'restart'. I tried it and it works for me. – tdelaney Jul 30 '13 at 23:23
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.

Ian Rose
  • 667
  • 1
  • 6
  • 18
  • 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. – Adilicious Apr 03 '13 at 13:48