From d7e89641fff8da2f7e10fb524f096aee2ba53a99 Mon Sep 17 00:00:00 2001 From: joknarf Date: Sat, 28 Jan 2023 11:23:13 +0100 Subject: [PATCH] pid_zero fix --- pgtree/pgtree.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pgtree/pgtree.py b/pgtree/pgtree.py index 3e8d739..ff3c50d 100755 --- a/pgtree/pgtree.py +++ b/pgtree/pgtree.py @@ -106,7 +106,8 @@ class Proctree: """ # pylint: disable=R0913 - def __init__(self, use_uid=False, use_ascii=False, use_color=False, psfield=None): + def __init__(self, use_uid=False, use_ascii=False, use_color=False, + pid_zero=True, psfield=None): """constructor""" self.pids = ('1') self.ps_info = {} # ps command info stored @@ -115,9 +116,9 @@ def __init__(self, use_uid=False, use_ascii=False, use_color=False, psfield=None self.pids_tree = {} self.top_parents = [] self.treedisp = Treedisplay(use_ascii, use_color) - self.get_psinfo(use_uid, psfield) + self.get_psinfo(use_uid, psfield, pid_zero) - def get_psinfo(self, use_uid, psfield): + def get_psinfo(self, use_uid, psfield, pid_zero): """parse unix ps command""" osname = platform.system() stime = 'stime' @@ -162,6 +163,9 @@ def get_psinfo(self, use_uid, psfield): 'comm': comm, 'args': args, } + if not pid_zero: + del self.ps_info['0'] + del self.children['0'] def pgrep(self, argv): """mini built-in pgrep if pgrep command not available @@ -271,7 +275,7 @@ def _print_tree(self, pids, print_it=True, pre=' '): def print_tree(self, pids=None, child_only=False, sig=0, confirmed=False): """display full or children only process tree""" - self.pids = pids or '0' if '0' in self.children else '1' + self.pids = pids or ('0' if '0' in self.children else '1') self.build_tree() if sig: self.kill_with_children(sig=sig, confirmed=confirmed) @@ -374,9 +378,6 @@ def main(argv): sig = 9 elif opt == "-p": found = arg.split(',') - elif opt == "-1": - found = "1" - options["-c"] = True elif opt == "-O": psfield = arg elif opt in ("-f", "-x", "-v", "-i", "-n", "-o"): @@ -389,7 +390,9 @@ def main(argv): ptree = Proctree(use_uid='-I' in options, use_ascii='-a' in options, - use_color=colored(options['-C']), psfield=psfield) + use_color=colored(options['-C']), + pid_zero='-1' not in options, + psfield=psfield) if pgrep_args: found = ptree.pgrep(pgrep_args)