def runshellcommand(script, env): # double-fork to completely detach from the parent process # based on http://code.activestate.com/recipes/278731 pid = os.fork() if pid: # parent return # subprocess.Popen() forks again, all we need to add is # flag the new process as a new session. if sys.version_info < (3, 2): newsession = {'preexec_fn': os.setsid} else: newsession = {'start_new_session': True} try: # connect std* to devnull to make sure the subprocess can't # muck up these stream for mercurial. # Connect all the streams to be more close to Windows behavior # and pager will wait for scripts to end if we don't do that nullrfd = open(os.devnull, 'r') nullwfd = open(os.devnull, 'w') subprocess.Popen(procutil.tonativestr(script), shell=True, stdin=nullrfd, stdout=nullwfd, stderr=nullwfd, env=procutil.tonativeenv(env), close_fds=True, **newsession) finally: # mission accomplished, this child needs to exit and not # continue the hg process here. os._exit(0)
def runshellcommand(script, env): # we can't use close_fds *and* redirect stdin. I'm not sure that we # need to because the detached process has no console connection. subprocess.Popen(procutil.tonativestr(script), shell=True, env=procutil.tonativeenv(env), close_fds=True, creationflags=_creationflags)
def _systembackground(cmd, environ=None, cwd=None): ''' like 'procutil.system', but returns the Popen object directly so we don't have to wait on it. ''' cmd = procutil.quotecommand(cmd) env = procutil.shellenviron(environ) proc = subprocess.Popen(procutil.tonativestr(cmd), shell=True, close_fds=procutil.closefds, env=procutil.tonativeenv(env), cwd=pycompat.rapply(procutil.tonativestr, cwd)) return proc