Skip to content

Instantly share code, notes, and snippets.

@tav
Created June 13, 2009 20:16
Show Gist options
  • Select an option

  • Save tav/129419 to your computer and use it in GitHub Desktop.

Select an option

Save tav/129419 to your computer and use it in GitHub Desktop.
import os def run_execve(program, args=None, env=None): if args is None: args = [program] if env is None: env = {} # we cannot directly call ll_execve() because it replaces the # current process. fd_read, fd_write = os.pipe() childpid = os.fork() if childpid == 0: # in the child os.close(fd_read) os.dup2(fd_write, 1) # stdout os.close(fd_write) os.execve(program, args, env) assert 0, "should not arrive here" else: # in the parent os.close(fd_write) child_stdout = [] while True: data = os.read(fd_read, 4096) if not data: break # closed child_stdout.append(data) pid, status = os.waitpid(childpid, 0) os.close(fd_read) print "#", child_stdout print pid, status return status, ''.join(child_stdout) # run_execve('/usr/bin/which', ['/usr/bin/which', 'true']) run_execve('/bin/echo', ['/bin/echo', 'true'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment