diff --git a/src/rez/system.py b/src/rez/system.py index abdc6981e..25780c1e7 100644 --- a/src/rez/system.py +++ b/src/rez/system.py @@ -79,14 +79,18 @@ def shell(self): import subprocess as sp shell = None - # check parent process via ps - try: - args = ['ps', '-o', 'args=', '-p', str(os.getppid())] - proc = sp.Popen(args, stdout=sp.PIPE) - output = proc.communicate()[0] - shell = os.path.basename(output.strip().split()[0]).replace('-', '') - except Exception: - pass + parent_pid = os.getppid() + if parent_pid != 0: + # When run from inside docker without an interactive shell, + # the parent pid will be 0, which will cause "ps" to + # print an error message: "process ID out of range". + try: + args = ['ps', '-o', 'args=', '-p', str(parent_pid)] + proc = sp.Popen(args, stdout=sp.PIPE) + output = proc.communicate()[0] + shell = os.path.basename(output.strip().split()[0]).replace('-', '') + except Exception: + pass # check $SHELL if shell not in shells: @@ -94,7 +98,7 @@ def shell(self): # traverse parent procs via /proc/(pid)/status if shell not in shells: - pid = str(os.getppid()) + pid = str(parent_pid) found = False while not found: