Python os.setsid() Examples
The following are 30
code examples of os.setsid().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os
, or try the search function
.
Example #1
Source File: hmi.py From minicps with MIT License | 6 votes |
def start_http_server(self, port=80): """Starts a simple http server on a choosen port. :port: integer defaults to 80 """ if(self.__http is None): cmd = "python -m SimpleHTTPServer %d" % port try: self.__http = Popen(cmd, shell=True, preexec_fn=setsid) logger.info( 'HMI%d - HTTP server started on port %d' % ( self.__id, port)) except OSError, e: emsg = repr(e) logger.warning( 'HMI%d - HTTP server cannot start: %s' % ( self.__id, emsg))
Example #2
Source File: pty.py From ironpython2 with Apache License 2.0 | 6 votes |
def fork(): """fork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.""" try: pid, fd = os.forkpty() except (AttributeError, OSError): pass else: if pid == CHILD: try: os.setsid() except OSError: # os.forkpty() already set us session leader pass return pid, fd master_fd, slave_fd = openpty() pid = os.fork() if pid == CHILD: # Establish a new session. os.setsid() os.close(master_fd) # Slave becomes stdin/stdout/stderr of child. os.dup2(slave_fd, STDIN_FILENO) os.dup2(slave_fd, STDOUT_FILENO) os.dup2(slave_fd, STDERR_FILENO) if (slave_fd > STDERR_FILENO): os.close (slave_fd) # Explicitly open the tty to make it become a controlling tty. tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) os.close(tmp_fd) else: os.close(slave_fd) # Parent and child process. return pid, master_fd
Example #3
Source File: test_process.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_mockPTYSetUid(self): """ Try creating a PTY process with setting its uid: it's almost the same path as the standard path, but with a C{switchUID} call before the exec. """ cmd = b'/mock/ouch' d = defer.Deferred() p = TrivialProcessProtocol(d) try: reactor.spawnProcess(p, cmd, [b'ouch'], env=None, usePTY=True, uid=8081) except SystemError: self.assertTrue(self.mockos.exited) self.assertEqual( self.mockos.actions, [('fork', False), 'setsid', ('setuid', 0), ('setgid', 0), ('switchuid', 8081, 1234), 'exec', ('exit', 1)]) else: self.fail("Should not be here")
Example #4
Source File: test_functional.py From cotyledon with Apache License 2.0 | 6 votes |
def setUp(self): super(Base, self).setUp() self.lines = [] self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(("127.0.0.1", 0)) self.t = threading.Thread(target=self.readlog) self.t.daemon = True self.t.start() examplepy = os.path.join(os.path.dirname(__file__), "examples.py") if os.name == 'posix': kwargs = { 'preexec_fn': os.setsid } else: kwargs = { 'creationflags': subprocess.CREATE_NEW_PROCESS_GROUP } self.subp = subprocess.Popen(['python', examplepy, self.name, str(self.sock.getsockname()[1])], **kwargs)
Example #5
Source File: video_recorder.py From DRL_DeliveryDuel with MIT License | 6 votes |
def start(self): self.cmdline = (self.backend, '-nostats', '-loglevel', 'error', # suppress warnings '-y', '-r', '%d' % self.frames_per_sec, # input '-f', 'rawvideo', '-s:v', '{}x{}'.format(*self.wh), '-pix_fmt',('rgb32' if self.includes_alpha else 'rgb24'), '-i', '-', # this used to be /dev/stdin, which is not Windows-friendly # output '-vcodec', 'libx264', '-pix_fmt', 'yuv420p', self.output_path ) logger.debug('Starting ffmpeg with "%s"', ' '.join(self.cmdline)) if hasattr(os,'setsid'): #setsid not present on Windows self.proc = subprocess.Popen(self.cmdline, stdin=subprocess.PIPE, preexec_fn=os.setsid) else: self.proc = subprocess.Popen(self.cmdline, stdin=subprocess.PIPE)
Example #6
Source File: misc.py From b2bua with BSD 2-Clause "Simplified" License | 6 votes |
def daemonize(logfile = None): # Fork once if os.fork() != 0: os._exit(0) # Create new session os.setsid() if os.fork() != 0: os._exit(0) os.chdir('/') fd = os.open('/dev/null', os.O_RDWR) os.dup2(fd, sys.__stdin__.fileno()) if logfile != None: fake_stdout = open(logfile, 'a', 1) sys.stdout = fake_stdout sys.stderr = fake_stdout fd = fake_stdout.fileno() os.dup2(fd, sys.__stdout__.fileno()) os.dup2(fd, sys.__stderr__.fileno()) if logfile == None: os.close(fd)
Example #7
Source File: backup.py From qubes-core-admin with GNU Lesser General Public License v2.1 | 6 votes |
def launch_proc_with_pty(args, stdin=None, stdout=None, stderr=None, echo=True): """Similar to pty.fork, but handle stdin/stdout according to parameters instead of connecting to the pty :return tuple (subprocess.Popen, pty_master) """ def set_ctty(ctty_fd, master_fd): os.setsid() os.close(master_fd) fcntl.ioctl(ctty_fd, termios.TIOCSCTTY, 0) if not echo: termios_p = termios.tcgetattr(ctty_fd) # termios_p.c_lflags termios_p[3] &= ~termios.ECHO termios.tcsetattr(ctty_fd, termios.TCSANOW, termios_p) (pty_master, pty_slave) = os.openpty() # pylint: disable=not-an-iterable p = yield from asyncio.create_subprocess_exec(*args, stdin=stdin, stdout=stdout, stderr=stderr, preexec_fn=lambda: set_ctty(pty_slave, pty_master)) os.close(pty_slave) return p, open(pty_master, 'wb+', buffering=0)
Example #8
Source File: utils.py From parasol with MIT License | 6 votes |
def start(self): self.cmdline = (self.backend, '-nostats', '-loglevel', 'error', # suppress warnings '-y', '-r', '%d' % self.frames_per_sec, # input '-f', 'rawvideo', '-s:v', '{}x{}'.format(*self.wh), '-pix_fmt',('rgb32' if self.includes_alpha else 'rgb24'), '-i', '-', # this used to be /dev/stdin, which is not Windows-friendly # output '-vcodec', 'libx264', '-pix_fmt', 'yuv420p', self.output_path ) logger.debug('Starting ffmpeg with "%s"', ' '.join(self.cmdline)) if hasattr(os,'setsid'): #setsid not present on Windows self.proc = subprocess.Popen(self.cmdline, stdin=subprocess.PIPE, preexec_fn=os.setsid) else: self.proc = subprocess.Popen(self.cmdline, stdin=subprocess.PIPE)
Example #9
Source File: misc.py From rtp_cluster with BSD 2-Clause "Simplified" License | 6 votes |
def daemonize(logfile = None): # Fork once if os.fork() != 0: os._exit(0) # Create new session os.setsid() if os.fork() != 0: os._exit(0) os.chdir('/') fd = os.open('/dev/null', os.O_RDWR) os.dup2(fd, sys.__stdin__.fileno()) if logfile != None: fake_stdout = open(logfile, 'a', 1) sys.stdout = fake_stdout sys.stderr = fake_stdout fd = fake_stdout.fileno() os.dup2(fd, sys.__stdout__.fileno()) os.dup2(fd, sys.__stderr__.fileno()) if logfile == None: os.close(fd)
Example #10
Source File: sifter.py From sandsifter with BSD 3-Clause "New" or "Revised" License | 6 votes |
def start(self): self.command = "%s %s -%c -R %s -s %d" % \ ( INJECTOR, " ".join(self.settings.args), self.settings.synth_mode, "-0" if self.settings.root else "", self.settings.seed ) self.process = subprocess.Popen( "exec %s" % self.command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, preexec_fn=os.setsid )
Example #11
Source File: daemon.py From clickhouse-mysql-data-reader with MIT License | 5 votes |
def background(self): # first fork # root process waits for the child in order not to have zombies in the system pid = os.fork() if pid > 0: # parent - root process wait for first child and exits os.wait() sys.exit(0) # first child # setup own environment os.chdir(self.root) os.umask(0) os.setsid() # second fork # first-fork child produces the real worker process and exits # first-fork child is being waited now by root process pid = os.fork() if pid > 0: sys.exit(0) # worker signal.signal(signal.SIGINT, self.shutdown) signal.signal(signal.SIGTERM, self.shutdown) # handle pid file atexit.register(self.delete_pidfile) self.write_pidfile() # handle streams self.redirect_std_streams()
Example #12
Source File: interactive.py From screeps_console with MIT License | 5 votes |
def getProcess(self): if self.proc: return self.proc console_path = os.path.join(os.path.dirname(sys.argv[0]), 'console.py ') write_fd = self.loop.watch_pipe(self.onUpdate) self.proc = subprocess.Popen( [sys.executable + ' ' + console_path + ' ' + self.connectionname + ' json'], stdout=write_fd, preexec_fn=os.setsid, close_fds=True, shell=True) return self.proc
Example #13
Source File: cross_platform_process.py From sublime-gulp with MIT License | 5 votes |
def _preexec_val(self): return os.setsid if sublime.platform() != "windows" else None
Example #14
Source File: _fork_pty.py From pipenv with MIT License | 5 votes |
def pty_make_controlling_tty(tty_fd): '''This makes the pseudo-terminal the controlling tty. This should be more portable than the pty.fork() function. Specifically, this should work on Solaris. ''' child_name = os.ttyname(tty_fd) # Disconnect from controlling tty, if any. Raises OSError of ENXIO # if there was no controlling tty to begin with, such as when # executed by a cron(1) job. try: fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY) os.close(fd) except OSError as err: if err.errno != errno.ENXIO: raise os.setsid() # Verify we are disconnected from controlling tty by attempting to open # it again. We expect that OSError of ENXIO should always be raised. try: fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY) os.close(fd) raise PtyProcessError("OSError of errno.ENXIO should be raised.") except OSError as err: if err.errno != errno.ENXIO: raise # Verify we can open child pty. fd = os.open(child_name, os.O_RDWR) os.close(fd) # Verify we now have a controlling tty. fd = os.open("/dev/tty", os.O_WRONLY) os.close(fd)
Example #15
Source File: hontel.py From hontel with MIT License | 5 votes |
def session_start(self): self._log("SESSION_START") self.process = subprocess.Popen(SHELL, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, preexec_fn=os.setsid) flags = fcntl.fcntl(self.process.stdout, fcntl.F_GETFL) fcntl.fcntl(self.process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
Example #16
Source File: benchmark_pipe.py From truvari with MIT License | 5 votes |
def cmd_exe(cmd, timeout=-1): """ Executes a command through the shell. timeout in minutes! so 1440 mean is 24 hours. -1 means never returns (ret_code, stdout, stderr, datetime) where ret_code is the exit code for the command executed stdout/err is the Standard Output Error from the command and datetime is a datetime object of the execution time """ t_start = time.time() proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, preexec_fn=os.setsid) signal.signal(signal.SIGALRM, alarm_handler) if timeout > 0: signal.alarm(int(timeout * 60)) try: stdoutVal, stderrVal = proc.communicate() signal.alarm(0) # reset the alarm except Alarm: logging.error(("Command was taking too long. " "Automatic Timeout Initiated after %d"), timeout) os.killpg(proc.pid, signal.SIGTERM) proc.kill() return 214, None, None, datetime.timedelta(seconds=int(time.time() - t_start)) t_end = time.time() stdoutVal = bytes.decode(stdoutVal) retCode = proc.returncode return retCode, stdoutVal, stderrVal, datetime.timedelta(seconds=int(t_end - t_start))
Example #17
Source File: bash.py From airflow with Apache License 2.0 | 5 votes |
def poke(self, context): """ Execute the bash command in a temporary directory which will be cleaned afterwards """ bash_command = self.bash_command self.log.info("Tmp dir root location: \n %s", gettempdir()) with TemporaryDirectory(prefix='airflowtmp') as tmp_dir: with NamedTemporaryFile(dir=tmp_dir, prefix=self.task_id) as f: f.write(bytes(bash_command, 'utf_8')) f.flush() fname = f.name script_location = tmp_dir + "/" + fname self.log.info("Temporary script location: %s", script_location) self.log.info("Running command: %s", bash_command) resp = Popen( # pylint: disable=subprocess-popen-preexec-fn ['bash', fname], stdout=PIPE, stderr=STDOUT, close_fds=True, cwd=tmp_dir, env=self.env, preexec_fn=os.setsid) self.log.info("Output:") for line in iter(resp.stdout.readline, b''): line = line.decode(self.output_encoding).strip() self.log.info(line) resp.wait() self.log.info("Command exited with return code %s", resp.returncode) return not resp.returncode
Example #18
Source File: base_task_runner.py From airflow with Apache License 2.0 | 5 votes |
def run_command(self, run_with=None): """ Run the task command. :param run_with: list of tokens to run the task command with e.g. ``['bash', '-c']`` :type run_with: list :return: the process that was run :rtype: subprocess.Popen """ run_with = run_with or [] full_cmd = run_with + self._command self.log.info("Running on host: %s", get_hostname()) self.log.info('Running: %s', full_cmd) # pylint: disable=subprocess-popen-preexec-fn proc = subprocess.Popen( full_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, close_fds=True, env=os.environ.copy(), preexec_fn=os.setsid ) # Start daemon thread to read subprocess logging output log_reader = threading.Thread( target=self._read_task_logs, args=(proc.stdout,), ) log_reader.daemon = True log_reader.start() return proc
Example #19
Source File: test_process.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def setsid(self): """ Fake C{os.setsid}. Save action. """ self.actions.append('setsid')
Example #20
Source File: ibash.py From vy with MIT License | 5 votes |
def start(self): print('(ibash) Bash process started...') self.child = Popen(self.cmd, shell=0, stdout=PIPE, stdin=PIPE, preexec_fn=setsid, stderr=STDOUT, env=environ) self.stdout = Device(self.child.stdout) self.stdin = Device(self.child.stdin) Stdout(self.stdout) Stdin(self.stdin) xmap(self.stdout, LOAD, lambda con, data: sys.stdout.write(data.decode('utf8'))) xmap(self.stdin, CLOSE, lambda dev, err: lose(dev)) xmap(self.stdout, CLOSE, lambda dev, err: lose(dev))
Example #21
Source File: launcher.py From modmail with GNU Affero General Public License v3.0 | 5 votes |
def start(self): if self.is_active: print(f"[Cluster {self.id}] Already active.") return self.started_at = time.time() self._process = await asyncio.create_subprocess_shell( self.command, stdin=asyncio.subprocess.DEVNULL, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, preexec_fn=os.setsid, limit=1024 * 256, ) self.status = "running" self.started_at = time.time() print(f"[Cluster {self.id}] The cluster is starting.") await asyncio.wait([self.read_stream(self._process.stdout), self.read_stream(self._process.stderr)]) return self
Example #22
Source File: pty.py From oss-ftp with MIT License | 5 votes |
def fork(): """fork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.""" try: pid, fd = os.forkpty() except (AttributeError, OSError): pass else: if pid == CHILD: try: os.setsid() except OSError: # os.forkpty() already set us session leader pass return pid, fd master_fd, slave_fd = openpty() pid = os.fork() if pid == CHILD: # Establish a new session. os.setsid() os.close(master_fd) # Slave becomes stdin/stdout/stderr of child. os.dup2(slave_fd, STDIN_FILENO) os.dup2(slave_fd, STDOUT_FILENO) os.dup2(slave_fd, STDERR_FILENO) if (slave_fd > STDERR_FILENO): os.close (slave_fd) # Explicitly open the tty to make it become a controlling tty. tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) os.close(tmp_fd) else: os.close(slave_fd) # Parent and child process. return pid, master_fd
Example #23
Source File: test_process.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_mockForkTTY(self): """ Test a TTY spawnProcess: check the path of the client code: fork, exec, exit. """ cmd = b'/mock/ouch' d = defer.Deferred() p = TrivialProcessProtocol(d) self.assertRaises(SystemError, reactor.spawnProcess, p, cmd, [b'ouch'], env=None, usePTY=True) self.assertTrue(self.mockos.exited) self.assertEqual( self.mockos.actions, [("fork", False), "setsid", "exec", ("exit", 1)])
Example #24
Source File: pty.py From Computable with MIT License | 5 votes |
def fork(): """fork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.""" try: pid, fd = os.forkpty() except (AttributeError, OSError): pass else: if pid == CHILD: try: os.setsid() except OSError: # os.forkpty() already set us session leader pass return pid, fd master_fd, slave_fd = openpty() pid = os.fork() if pid == CHILD: # Establish a new session. os.setsid() os.close(master_fd) # Slave becomes stdin/stdout/stderr of child. os.dup2(slave_fd, STDIN_FILENO) os.dup2(slave_fd, STDOUT_FILENO) os.dup2(slave_fd, STDERR_FILENO) if (slave_fd > STDERR_FILENO): os.close (slave_fd) # Explicitly open the tty to make it become a controlling tty. tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) os.close(tmp_fd) else: os.close(slave_fd) # Parent and child process. return pid, master_fd
Example #25
Source File: daemonize.py From Computable with MIT License | 5 votes |
def daemonize(): # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16 if os.fork(): # launch child and... os._exit(0) # kill off parent os.setsid() if os.fork(): # launch child and... os._exit(0) # kill off parent again. null = os.open('/dev/null', os.O_RDWR) for i in range(3): try: os.dup2(null, i) except OSError as e: if e.errno != errno.EBADF: raise os.close(null)
Example #26
Source File: pty.py From BinderFilter with MIT License | 5 votes |
def fork(): """fork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.""" try: pid, fd = os.forkpty() except (AttributeError, OSError): pass else: if pid == CHILD: try: os.setsid() except OSError: # os.forkpty() already set us session leader pass return pid, fd master_fd, slave_fd = openpty() pid = os.fork() if pid == CHILD: # Establish a new session. os.setsid() os.close(master_fd) # Slave becomes stdin/stdout/stderr of child. os.dup2(slave_fd, STDIN_FILENO) os.dup2(slave_fd, STDOUT_FILENO) os.dup2(slave_fd, STDERR_FILENO) if (slave_fd > STDERR_FILENO): os.close (slave_fd) # Explicitly open the tty to make it become a controlling tty. tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) os.close(tmp_fd) else: os.close(slave_fd) # Parent and child process. return pid, master_fd
Example #27
Source File: tools.py From Dallinger with MIT License | 5 votes |
def _boot(self): # Child processes don't start without a HOME dir if not self.env.get("HOME", False): raise HerokuStartupError('"HOME" environment not set... aborting.') port = self.config.get("base_port") web_dynos = self.config.get("num_dynos_web") worker_dynos = self.config.get("num_dynos_worker") commands = [ self.shell_command, "local", "-p", str(port), "web={},worker={}".format(web_dynos, worker_dynos), ] try: options = { "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT, "env": self.env, "preexec_fn": os.setsid, } if six.PY3: options["encoding"] = "utf-8" self._process = subprocess.Popen(commands, **options) except OSError: self.out.error("Couldn't start Heroku for local debugging.") raise
Example #28
Source File: lib.py From edgedb with Apache License 2.0 | 5 votes |
def detach_process_context(): """Datach process context. Does it in three steps: 1. Forks and exists parent process. This detaches us from shell, and since the child will have a new PID but will inherit the Group PID from parent, the new process will not be a group leader. 2. Call 'setsid' to create a new session. This makes the process a session leader of a new session, process becomes the process group leader of a new process group and it doesn't have a controlling terminal. 3. Form and exit parent again. This guarantees that the daemon is not a session leader, which prevents it from acquiring a controlling terminal. Reference: “Advanced Programming in the Unix Environment”, section 13.3, by W. Richard Stevens. """ def fork_and_exit_parent(error_message): try: if os.fork() > 0: # Don't need to call 'sys.exit', as we don't want to # run any python interpreter clean-up handlers os._exit(0) except OSError as ex: raise DaemonError( '{}: [{}] {}'.format(error_message, ex.errno, ex.strerror)) from ex fork_and_exit_parent(error_message='Failed the first fork') os.setsid() fork_and_exit_parent(error_message='Failed the second fork')
Example #29
Source File: launch_benchmark.py From models with Apache License 2.0 | 5 votes |
def _launch_command(self, run_cmd): """runs command that runs the start script in a container or on bare metal and exits on ctrl c""" p = subprocess.Popen(run_cmd, preexec_fn=os.setsid) try: p.communicate() except KeyboardInterrupt: os.killpg(os.getpgid(p.pid), signal.SIGKILL)
Example #30
Source File: integ_test_base.py From TabPy with MIT License | 5 votes |
def setUp(self): super(IntegTestBase, self).setUp() prefix = "TabPy_IntegTest_" self.tmp_dir = tempfile.mkdtemp(prefix=prefix) # create temporary state.ini orig_state_file_name = os.path.abspath( self._get_state_file_path() + "/state.ini" ) self.state_file_name = os.path.abspath(self.tmp_dir + "/state.ini") if orig_state_file_name != self.state_file_name: shutil.copyfile(orig_state_file_name, self.state_file_name) # create config file orig_config_file_name = os.path.abspath(self._get_config_file_name()) self.config_file_name = os.path.abspath( self.tmp_dir + "/" + os.path.basename(orig_config_file_name) ) if orig_config_file_name != self.config_file_name: shutil.copyfile(orig_config_file_name, self.config_file_name) # Platform specific - for integration tests we want to engage # startup script with open(self.tmp_dir + "/output.txt", "w") as outfile: cmd = ["tabpy", "--config=" + self.config_file_name] preexec_fn = None if platform.system() == "Windows": self.py = "python" else: self.py = "python3" preexec_fn = os.setsid coverage.process_startup() self.process = subprocess.Popen( cmd, preexec_fn=preexec_fn, stdout=outfile, stderr=outfile ) # give the app some time to start up... time.sleep(5)