I'm trying to execute a shellscript from Python that takes ipaddress as a parameter,
I'm using the below command but get an error, I need to execute this shellscript as a Sudo user..
Error:-
[root@linuxhost web]# python test.py
29575
usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user
name|#uid]
process = subprocess.Popen(['sudo','/usr/local/bin/test.sh','127.0.0.1'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
process.wait()
I tried calling the shellscript by directly calling using sudo and still it fails..
process = subprocess.Popen(['sudo /usr/local/bin/test.sh','127.0.0.1'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
asked Feb 24, 2014 at 22:52
user1050619
21k89 gold badges255 silver badges432 bronze badges
1 Answer 1
You can't combine list args with shell=True. Use something like
process = subprocess.Popen('sudo /usr/local/bin/test.sh 127.0.0.1',
stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
or do not use shell=True
process = subprocess.Popen(['sudo','/usr/local/bin/test.sh','127.0.0.1'],
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
answered Feb 24, 2014 at 22:55
Krumelur
32.8k10 gold badges82 silver badges123 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Krumelur
You mean none of them are? What are the problems you encounter?
user1050619
with list args and no shell=True, I get this error------ File "/usr/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
Krumelur
This usually means the executable was not found. Try
/usr/bin/sudo instead of just sudo (in case sudo is not in your path).lang-py
subprocess.PIPEunless you consume the pipes. It may stall the child process.