def callback(signal, frame): if onlyonce and self.exited: return if cascade: if os.getpgrp() == os.getpid(): os.killpg(os.getpgrp(), signal) if onlyonce: self.exited = True _callback(signal, frame)
def switch_pgid(self): try: if os.getpgrp() != os.tcgetpgrp(0): self.__old_pgid = os.getpgrp() os.setpgid(0, os.tcgetpgrp(0)) else: self.__old_pgid = None except OSError: self.__old_pgid = None
def spawn_daemon(fork=None, pgrpfile=None, outfile='out.txt'): 'causes run to be executed in a newly spawned daemon process' global LAST_PGRP_PATH fork = fork or os.fork open(outfile, 'a').close() # TODO: configurable output file if pgrpfile and os.path.exists(pgrpfile): try: cur_pid = int(open(pgrpfile).read().rstrip("\n")) os.killpg(cur_pid, 0) raise Exception("arbiter still running with pid:" + str(cur_pid)) except (OSError, ValueError): pass if fork(): # return True means we are in parent return True else: os.setsid() # break association with terminal via new session id if fork(): # fork one more layer to ensure child will not re-acquire terminal os._exit(0) LAST_PGRP_PATH = pgrpfile if pgrpfile: with open(pgrpfile, 'w') as f: f.write(str(os.getpgrp()) + "\n") logging.root.addHandler(SysLogHandler()) rotating_out = RotatingStdoutFile(outfile) rotating_out.start() return False # return False means we are in daemonized process
def handle_winch(self): "SIGWINCH handling" if os.getppid() == 1 or os.getpgrp() != os.getpid(): self.logger.info("graceful stop of workers") self.kill_workers(True) else: self.log.info("SIGWINCH ignored. Not daemonized")
def test_os_getpgrp(): def does_stuff(): return os.getpgrp() f1 = compile(does_stuff, []) res = f1() assert res == os.getpgrp()
def __init__(self, pid_list): """Create new ResourceMonitor instance.""" self.pid_list = [] self.ignore_pid_list = pid_list self.ignore_pid_list.append(getpid()) self.process_group_id = getpgrp()
def posix_getpgrp(space): """ posix_getpgrp - Return the current process group identifier """ try: return space.newint(os.getpgrp()) except OSError, e: space.set_errno(e.errno) return space.newbool(False)
def become_tty_fg(self,child=True): os.setpgrp() hdlr = signal.signal(signal.SIGTTOU, signal.SIG_IGN) tty = os.open('/dev/tty', os.O_RDWR) os.tcsetpgrp(tty, os.getpgrp()) if child: signal.signal(signal.SIGTTOU, hdlr)
def rest_som_start(host,params,callback=None): params = json.loads(params) maskid = params.get('maskid',None) stackid = params.get('stackid') projectid = params.get('projectid') xdim = int(params.get('xdim',10)) ydim = int(params.get('ydim',2)) radius = int(params.get('radius',1)) levels = int(params.get('levels',3)) iters = int(params.get('iters',5000)) alpha = float(params.get('alpha',0.01)) hed,img = rest_queryStackPath(projectid,stackid) mask = pathFromId(maskid,ext='.png') print 'parent group process id:',os.getpgrp() jobid = hashargs([hed,img,mask,xdim,ydim,radius,levels,iters,alpha]) job = mp.Process(target=som_start,args=[host,hed,img,mask,xdim,ydim,radius,levels,iters,alpha]) job.start() callback({ 'id' : jobid, 'url' : rest_urlForJobId(host,jobid), })
def get_proc_info(): ret = {} ret['pid'] = os.getpid() _user_t, _sys_t = os.times()[:2] ret['cpu_times'] = {'user_time': _user_t, 'sys_time': _sys_t} ret['cwd'] = os.getcwdu() ret['umask'] = os.umask(os.umask(2)) # have to set to get ret['umask_str'] = '{0:03o}'.format(ret['umask']) try: import getpass ret['owner'] = getpass.getuser() except: try: ret['owner'] = os.getuid() except: pass try: # unix-only ret['ppid'] = os.getppid() ret['pgid'] = os.getpgrp() # add 0 to get current nice # also, seems to return process group's nice level ret['niceness'] = os.nice(0) except AttributeError: pass ret['rusage'] = get_rusage_dict() ret['rlimit'] = get_rlimit_dict() return ret
def run(): if os.name == "nt": sys.stdout = io.TextIOWrapper( sys.stdout.detach(), encoding=sys.stdout.encoding, errors="backslashreplace", line_buffering=True) sys.stderr = io.TextIOWrapper( sys.stderr.detach(), encoding=sys.stderr.encoding, errors="backslashreplace", line_buffering=True) if hasattr(os, 'getpgrp'): f = open('mentionsbot.pgid', 'w') f.write(str(os.getpgrp())) f.close() def signal_handler(singal, frame): del singal, frame f = open('mentionsbot.pid', 'w') f.close() print("Got signal, killing everything") os._exit(0) # kill everyone tmlog = logging.getLogger("track_mentions") tmlog.setLevel(logging.DEBUG) tmlog.addHandler(logging.StreamHandler()) signal.signal(signal.SIGINT, signal_handler) instance = MentionsBot(command_prefix=bare_pms) instance.remove_command("help") instance.run('token', config.token)
def main(actions=None): # NOTE(agordeev): get its own process group by calling setpgrp. # Process group is used to distribute signals to subprocesses. # The main application is already a process group leader, # then there's no need to call os.setpgrp if os.getpid() != os.getpgrp(): os.setpgrp() signal.signal(signal.SIGTERM, handle_sigterm) # Setup logging and process configuration options logging.register_options(CONF) CONF.register_cli_opts(cli_opts) CONF(sys.argv[1:], project=PROJECT, version=version.version_info.release_string()) logging.setup(CONF, PROJECT) try: if CONF.input_data: data = yaml.safe_load(CONF.input_data) else: with open(CONF.input_data_file) as f: data = yaml.safe_load(f) LOG.debug('Input data: %s', data) mgr = manager.Manager(data) if actions: for action in actions: getattr(mgr, action)() except Exception as exc: handle_exception(exc)
def juju(host, args): run_command('juju --version') logging.info("Juju home is set to {}".format(host.tmp_juju_home)) bootstrapped = [] try: for model in host.models: try: run_command( 'juju bootstrap --show-log -e {} --constraints mem=4G'. format(model)) run_command('juju set-constraints -e {} mem=2G'.format(model)) except subprocess.CalledProcessError: logging.error('Bootstrapping failed on {}'.format(model)) continue bootstrapped.append(model) host.models = bootstrapped yield finally: if os.getegid() == 111: run_command('sudo chown -R jenkins:jenkins {}'.format(host.root)) else: run_command('sudo chown -R {}:{} {}'.format( os.getegid(), os.getpgrp(), host.root)) try: copy_remote_logs(host.models, args) except subprocess.CalledProcessError: logging.error('Getting logs failed.') for model in host.models: try: run_command( 'juju destroy-environment --force --yes {}'.format(model)) except subprocess.CalledProcessError: logging.error("Error destroy env failed: {}".format(model))
def daemonize(): """ daemonize: Purpose: Detach from stdin/stdout/stderr, return control of the term to the user. Returns: Nothing. """ if os.name == "nt" or os.name == "ce": # No way to fork or daemonize on windows. Just do nothing for now? return if not os.fork(): # get our own session and fixup std[in,out,err] os.setsid() sys.stdin.close() sys.stdout = NullDevice() sys.stderr = NullDevice() if not os.fork(): # hang around till adopted by init while os.getppid() == os.getpgrp(): time.sleep(0.5) else: # time for child to die os._exit(0) else: # wait for child to die and then bail os.wait() sys.exit()
def main(aargv): do_not_run = False forcerepomd = False verbose = 0 argindex = 1 retval = -1 while argindex < len(aargv) and aargv[argindex][0] == '-': if aargv[argindex] == '-v': verbose += 1 elif aargv[argindex] == '-n': do_not_run = True argindex += 1 if argindex + 4 != len(aargv): print('packbuild.py [-v] [-n] [-r] [-b] <chroot_dirname> <archname> <projectname> <packagename>') sys.exit(1) context = sourcerepo.PackageContext(aargv[argindex:-1], verbose, 'i586', forcerepomd) genutil.startprocessing(aargv[argindex+3]) packagedir = customization.buildbase + context.buildproject + '/' + aargv[argindex+3] if not os.path.exists(packagedir + '._manifest'): print('package not found', packagedir) return if ParseOBS.ParsePackageMeta(packagedir + '._meta').disabled(context.archtype): print("package disabled", aargv[argindex+3]) genutil.exitprocessing(15) if context.initok: if aargv[argindex+3] in customization.bannedpackages: print('ignorepackagebuild... !!!:', aargv[argindex+3]) genutil.exitprocessing(20) print("*********************** ", aargv[argindex+3], aargv[argindex], os.getpgrp()) retval = rpmbuild_one_directory(context, do_not_run, aargv[argindex+3]) genutil.exitprocessing(retval) return 0
def watch_memory(controller_pid, max_memory, max_age): if max_age: end_time = time.time() + max_age else: end_time = None process_group = os.getpgrp() while True: if max_age: now = time.time() if now + MEMORY_WATCH_INTERVAL > end_time: time.sleep(end_time - now) print "(%s) *** watcher restarting processes! Time limit exceeded." % ( os.getpid(), ) os.kill(controller_pid, signal.SIGHUP) end_time = time.time() + max_age continue time.sleep(MEMORY_WATCH_INTERVAL) if max_memory: out = commands.getoutput('ps -o rss -g %s' % (process_group, )) used_mem = sum(int(x) for x in out.split('\n')[1:]) if used_mem > max_memory: print "(%s) *** memory watcher restarting processes! Memory usage of %s exceeded %s." % ( os.getpid(), used_mem, max_memory) os.kill(controller_pid, signal.SIGHUP)
def cleanup(self): pgrp = os.getpgrp() while True: try: pid, status = os.waitpid(-pgrp, os.WNOHANG) except OSError: break
def IsRunFromShellScript(): """Check if command is being run from command line or a script.""" # Commands run from a shell script typically have getppid() == getpgrp() if platforms.OperatingSystem.Current() != platforms.OperatingSystem.WINDOWS: if os.getppid() == os.getpgrp(): return True return False
def _GetPidForLock(): """Returns the PID used for host_forwarder initialization. The PID of the "sharder" is used to handle multiprocessing. The "sharder" is the initial process that forks that is the parent process. """ return os.getpgrp()
def juju(host, args): run_command('juju --version') logging.info("Juju home is set to {}".format(host.tmp_juju_home)) try: for model in host.models: run_command( 'juju bootstrap --show-log -e {} --constraints mem=4G'.format( model)) run_command('juju set-constraints -e {} mem=2G'.format(model)) yield finally: if os.getegid() == 111: run_command('sudo chown -R jenkins:jenkins {}'.format(host.root)) else: run_command('sudo chown -R {}:{} {}'.format( os.getegid(), os.getpgrp(), host.root)) error = None copy_remote_logs(host.models, args) for model in host.models: try: run_command( 'juju destroy-environment --force --yes {}'.format(model)) except subprocess.CalledProcessError as e: error = e logging.error("Error destroy env failed: {}".format(model)) if error: raise error
def say(self, text, timeout=5): for p in '"-!\'': text = text.replace(p, '') if _platform == "linux" or _platform == "linux2": # linux pass elif _platform == "darwin": # OS X subprocess.call(['say', text]) pass elif _platform == "win32": # Windows... print('windows detected') pid = os.fork() if pid == 0: os.setpgrp() new = os.fork() if new == 0: subprocess.call(['python', 'speech.py', text]) sys.exit(1) time.sleep(timeout) os.kill(-int(os.getpgrp()), signal.SIGKILL) sys.exit(1) os.waitpid(pid, 0) # time.sleep(5) # os.kill(pid, signal.SIGKILL) # os.kill(-int(os.getpgrp()), signal.SIGKILL) # self.engine.say(text) # self.engine.runAndWait() print(text) pass
def getppid(pid=None, host=None, group=False): # find parent of pid '''get parent process id (ppid) for the given process If pid is None, the pid of the __main__ python instance will be used. Inputs: pid -- process id host -- hostname where process is running group -- get parent group id (pgid) instead of direct parent id? ''' if pid is None: if host: raise OSError('[Error 3] No such process') return os.getpgrp() if group else os.getppid() pid = str(pid) command = "ps axj" response = execute(command, host).response() if response is None: raise OSError('[Errno 3] No such process') # analyze header for correct pattern and indx head = (line for line in response.split('\n') if 'PPID' in line) try: head = head.next().split() except StopIteration: raise OSError('Failure to recover process id') parent = 'PGID' if group else 'PPID' indx = (head.index('PID'), head.index(parent)) # extract good data lines from response response = _psax(response, pattern=_psaxj) # select the PID and parent id response = dict(_select(line,indx) for line in response.split('\n') if line) response = response.get(pid, None) if response is None: raise OSError('[Errno 3] No such process') return int(response)
def makeThisProcessAsDaemon(): """The code, as it is, will create a new file in the root directory, when executed with superuser privileges. The file will contain the following daemon related process parameters: return code, process ID, parent process group ID, session ID, user ID, effective user ID, real group ID, and the effective group ID.""" try: retCode=0 retCode = utils.daemonProcess.create() logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', filename= utils.logDirectory + 'semfsDaemon.log', filemode='w') ProcParams = \ """ return code=%s \ process ID=%s \ parent process ID=%s \ process group ID=%s \ session ID=%s \ user ID=%s \ effective user ID=%s \ real group ID=%s \ effective group ID=%s """ \ % (retCode, os.getpid(), os.getppid(), os.getpgrp(), os.getsid(0), \ os.getuid(), os.geteuid(), os.getgid(), os.getegid()) logging.debug(ProcParams) os.chdir(utils.semFSInstalledDirectory) except: logging.info("Error while making the process as Daemon")
def _preExec(self): # Set process group logging.debug("CHILD: setpgid(0, 0)") os.setpgid(0, 0) if self.jobCtrl.foreground: logging.debug("CHILD: tcsetpgrp(0, %d)", os.getpgrp()) os.tcsetpgrp(0, os.getpgrp()) # Restore signal to default values signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL) signal.signal(signal.SIGTTOU, signal.SIG_DFL) signal.signal(signal.SIGTTIN, signal.SIG_DFL) signal.signal(signal.SIGTSTP, signal.SIG_DFL) signal.signal(signal.SIGCHLD, signal.SIG_DFL) signal.signal(signal.SIGCONT, signal.SIG_DFL)
def handler(signum, frame): if (signum == 14): sys.stdout.write("UNKNOWN - Timed out\n") # Commit suicide process_group = os.getpgrp() os.killpg(process_group, signal.SIGTERM) sys.exit(3)
def __init__(self, debug=False, out=stdout, errors=True): if not errors: errors = out self.out = (errors, out) self.proc = argv[0] self.pid = getpgrp() return self
def handle_winch(self): "SIGWINCH handling" if os.getppid() == 1 or os.getpgrp() != os.getpid(): log.info("graceful stop of workers") self.num_workers = 0 self.kill_workers(signal.SIGQUIT) else: log.info("SIGWINCH ignored. Not daemonized")
def ready(self): return self._event( event_type=ClientEventFactory.READY, payload={ "pid": os.getpid(), "pgroup_id": os.getpgrp() } )
def test_ioctl(self): # If this process has been put into the background, TIOCGPGRP returns # the session ID instead of the process group id. ids = (os.getpgrp(), os.getsid(0)) with open("/dev/tty", "rb") as tty: r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") rpgrp = struct.unpack("i", r)[0] self.assertIn(rpgrp, ids)
def handle_winch(self): """Window change.""" if os.getppid() == 1 or os.getpgrp() != os.getpid(): log.info("graceful stop of children") self.num_children = 0 self.kill_children(signal.SIGQUIT) else: log.info("SIGWINCH ignored. Not daemonized")
import os import time pid = os.fork() if pid: print("the main process euid is {}, gid is {}, id is {}, pgid is {}".format(os.geteuid(),os.getgid(), os.getpid(), os.getpgrp())) os._exit(0) # kill original process print("the child process euid is {}, gid is {}, id is {}, original pgid is {}".format(os.geteuid(),os.getgid(),os.getpid(), os.getpgrp())) print "daemon started, now change the pgid of child process and let it become the leader process of a new group" success = os.setpgrp() # success = os.setpgid(0, 0) if success: print "get some problem here" print("the child process euid is {}, gid is {}, id is {}, pgid is {}".format(os.geteuid(),os.getgid(),os.getpid(), os.getpgrp())) time.sleep(10) print "daemon terminated"
os.system("umount -l %s >/dev/null 2>&1" % p) def umount_all(): at = 0 while True: ml = getmlist() if len(ml) == 2 or at >= 8: return ml try_umont(ml) at += 1 ml = umount_all() print("Me:", os.getpid(), os.getpgrp(), os.getsid(0)) print("Left:", ml) print("Proc:", os.listdir("/proc")) # # From now on make's output will mess with # python print-s. Flush them into logfile # for easier reading # sys.stdout.flush() sys.stderr.flush() os.chdir(criu_tests_dir + "/live/") os.system("make cleanout") for tst in test_list: os.system("make -C %s %s.pid" % tuple(tst.rsplit("/", 1)))
def clusterLocal(opt,arg): """Start a cluster on the local machine.""" # Store all logs inside the ipython directory ipdir = cutils.getIpythonDir() pjoin = os.path.join logfile = opt.logfile if logfile is None: logdir_base = pjoin(ipdir,'log') ensureDir(logdir_base) logfile = pjoin(logdir_base,'ipcluster-') print 'Starting controller:', controller = Popen(['ipcontroller','--logfile',logfile]) print 'Controller PID:',controller.pid print 'Starting engines: ', time.sleep(3) englogfile = '%s%s-' % (logfile,controller.pid) engines = [ Popen(['ipengine','--logfile',englogfile]) for i in range(opt.n) ] eids = [e.pid for e in engines] print 'Engines PIDs: ',eids print 'Log files: %s*' % englogfile proc_ids = eids + [controller.pid] procs = engines + [controller] grpid = os.getpgrp() try: startMsg('127.0.0.1') print 'You can also hit Ctrl-C to stop it, or use from the cmd line:' print print 'kill -INT',grpid print try: while True: time.sleep(5) except: pass finally: print 'Stopping cluster. Cleaning up...' cleanup(stop,controller,engines) for i in range(4): time.sleep(i+2) nZombies = numAlive(controller,engines) if nZombies== 0: print 'OK: All processes cleaned up.' break print 'Trying again, %d processes did not stop...' % nZombies cleanup(kill,controller,engines) if numAlive(controller,engines) == 0: print 'OK: All processes cleaned up.' break else: print '*'*75 print 'ERROR: could not kill some processes, try to do it', print 'manually.' zombies = [] if controller.returncode is None: print 'Controller is alive: pid =',controller.pid zombies.append(controller.pid) liveEngines = [ e for e in engines if e.returncode is None ] for e in liveEngines: print 'Engine is alive: pid =',e.pid zombies.append(e.pid) print print 'Zombie summary:',' '.join(map(str,zombies))
def term(sig_num, addtion): """用于父进程异常退出时,将所有子进程都关闭""" logger.error('current pid is %s, group id is %s, exception interrupt' % (os.getpid(), os.getpgrp())) os.killpg(os.getpgid(os.getpid()), signal.SIGKILL)
from gi.repository import GLib import traceback try: import __builtin__ as builtins except ImportError: import builtins GREEN = lambda x: "\x1b[32;01m" + x + "\x1b[39;49;00m" BLUE = lambda x: "\x1b[34;01m" + x + "\x1b[39;49;00m" BOLD = lambda x: "\033[1m" + x + "\033[0m" YELLOW = lambda x: "\x1b[33;01m" + x + "\x1b[39;49;00m" import fcntl, struct, termios try: in_fg = os.getpgrp() == struct.unpack( str('h'), fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0] except IOError: in_fg = 'DEBUG' in os.environ def dprint(*args): #dont print if in the background if in_fg: s = "" for a in args: s += ("%s " % a) co = sys._getframe(1).f_code fname = BOLD(co.co_name)
def _handle_signal(signal_number, frame): # pragma: no cover ''' Send the signal to all processes in borgmatic's process group, which includes child process. ''' os.killpg(os.getpgrp(), signal_number)
def show_setting_prgrp(): print('Calling os.setpgrp() from {}'.format(os.getpid())) os.setpgrp() print('Process group is now {}'.format(os.getpgrp())) sys.stdout.flush()
def _signal_handler_(signal, frame): print "Kill workers and process group(pgid): " + str(os.getpgrp()) os.killpg(os.getpgrp(), 9) sys.exit(0)
def kill_the_group(signal_number: signal.Signals, frame: Any) -> None: os.killpg(os.getpgrp(), signal_number)