Take the 2-minute tour ×

When i try to execute a command with subprocess.Popen. I am getting this error;

In [3]: subprocess.Popen(["su - dhclient","eth0"])
OSError                                   Traceback (most recent call last)
/home/oae/<ipython-input-3-1cd768a39571> in <module>()
----> 1 subprocess.Popen(["su - dhclient","eth0"])

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    677                             p2cread, p2cwrite,
    678                             c2pread, c2pwrite,
--> 679                             errread, errwrite)
    681         if mswindows:

/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1247                     if fd is not None:
   1248                         os.close(fd)
-> 1249                 raise child_exception

OSError: [Errno 2] No such file or directory

How can i fix this? Thanks.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can use:

subprocess.Popen("su - dhclient eth0", shell=True)
share|improve this answer
thanks. this is working. i needed to add shell=True. –  O. Alperen Elhan Aug 10 '12 at 12:53
shell=True is generally frowned upon as a security issue so don't over use it. It is, however, the correct way to do it in this case. –  Ryan Haining Aug 10 '12 at 13:13
add comment

Your Answer


