Heyas
So I'm trying to get a process to be run as a super user from within a python script using subprocess. In the ipython shell something like
proc = subprocess.Popen('sudo apach2ctl restart', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
works fine, but as soon as I stick it into a script I start getting:
sudo: apach2ctl: command not found
I would guess is due to the way sudo handles environments on ubuntu. (I've also tried sudo -E apche2ctl restart
and sudo env path=$PATH apache2ctl restart
with no avail)
So my question is basically, if I want to run apache2ctl restart
as super user that prompts the user for the super user password when required, how should I go about doing this? I have no intention of storing passwords in the script.
Thanks!
Edit: I've tried passing in the commands as both a string and tokenized into a list. In the python interpreter, with a string I'll get the password prompt properly (still doesnt work in a python script as in my original problem), a list just gives the help screen for sudo.
Ahhh ok, it's a combination of the two. So what I gather is that while Popen will work with some commands just as strings when shell=True, it takes Popen(['sudo','/usr/sbin/apache2ctl','restart']) to get sudo to work.
Another note, no shell=True.
Thanks!