From 4033c97e5a069bdb27044c73619db3fef668f8ab Mon Sep 17 00:00:00 2001 From: Sylvain Pineau Date: Tue, 14 Jul 2020 12:42:49 +0200 Subject: bin:process_wait -> process_wait.py --- bin/process_wait | 73 ----------------------------------------------------- bin/process_wait.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 73 deletions(-) delete mode 100755 bin/process_wait create mode 100755 bin/process_wait.py (limited to 'bin') diff --git a/bin/process_wait b/bin/process_wait deleted file mode 100755 index 6107d1b..0000000 --- a/bin/process_wait +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python3 - -import os -import sys -import time - -from optparse import OptionParser -from subprocess import Popen, PIPE - - -COMMAND_FORMAT = "pgrep -f %(options)s %(process)s" - - -def process_pids(process, *options): - options_string = " ".join(options) - command = COMMAND_FORMAT % {"options": options_string, "process": process} - - # Exclude this process and the pgrep process - subprocess = Popen( - command, stdout=PIPE, shell=True, universal_newlines=True) - exclude_pids = [os.getpid(), os.getppid(), subprocess.pid] - - pids_string = subprocess.communicate()[0] - pids = [int(pid) for pid in pids_string.split()] - - result = set(pids).difference(exclude_pids) - return list(result) - - -def process_count(*args): - return len(process_pids(*args)) - - -def main(args): - default_sleep = 1 - - usage = "Usage: %prog PROCESS [PROCESS...]" - parser = OptionParser(usage=usage) - parser.add_option("-s", "--sleep", - type="int", - default=default_sleep, - help="Number of seconds to sleep between checks.") - parser.add_option("-t", "--timeout", - type="int", - help="Number of seconds to timeout from sleeping.") - parser.add_option("-u", "--uid", - help="Effective user name or id of the running processes") - (options, processes) = parser.parse_args(args) - - process_args = [] - if options.uid is not None: - process_args.extend(["-u", options.uid]) - - while True: - for process in processes: - if process_count(process, *process_args): - break - else: - break - - if options.timeout is not None: - if options.timeout <= 0: - return 1 - else: - options.timeout -= options.sleep - - time.sleep(options.sleep) - - return 0 - - -if __name__ == "__main__": - sys.exit(main(sys.argv[1:])) diff --git a/bin/process_wait.py b/bin/process_wait.py new file mode 100755 index 0000000..6107d1b --- /dev/null +++ b/bin/process_wait.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +import os +import sys +import time + +from optparse import OptionParser +from subprocess import Popen, PIPE + + +COMMAND_FORMAT = "pgrep -f %(options)s %(process)s" + + +def process_pids(process, *options): + options_string = " ".join(options) + command = COMMAND_FORMAT % {"options": options_string, "process": process} + + # Exclude this process and the pgrep process + subprocess = Popen( + command, stdout=PIPE, shell=True, universal_newlines=True) + exclude_pids = [os.getpid(), os.getppid(), subprocess.pid] + + pids_string = subprocess.communicate()[0] + pids = [int(pid) for pid in pids_string.split()] + + result = set(pids).difference(exclude_pids) + return list(result) + + +def process_count(*args): + return len(process_pids(*args)) + + +def main(args): + default_sleep = 1 + + usage = "Usage: %prog PROCESS [PROCESS...]" + parser = OptionParser(usage=usage) + parser.add_option("-s", "--sleep", + type="int", + default=default_sleep, + help="Number of seconds to sleep between checks.") + parser.add_option("-t", "--timeout", + type="int", + help="Number of seconds to timeout from sleeping.") + parser.add_option("-u", "--uid", + help="Effective user name or id of the running processes") + (options, processes) = parser.parse_args(args) + + process_args = [] + if options.uid is not None: + process_args.extend(["-u", options.uid]) + + while True: + for process in processes: + if process_count(process, *process_args): + break + else: + break + + if options.timeout is not None: + if options.timeout <= 0: + return 1 + else: + options.timeout -= options.sleep + + time.sleep(options.sleep) + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) -- cgit v1.2.3