I asked our friends from ChatGPT for a robust check.
It suggested the following:
import os
import subprocess
def parent_process_name():
ppid = os.getppid()
try:
return subprocess.check_output(
["ps", "-p", str(ppid), "-o", "comm="],
text=True
).strip()
except Exception:
return None
parent = parent_process_name()
print(parent) # likely "pwsh"
os.getppid should also be available on Windows. I am not so sure about ps. Depending on the availability of ps on Windows, this check can also be used on Windows, otherwise only on unix systems.
update: It is not available on Windows.
I think this is an improvement compared to the current heuristics.
I asked our friends from ChatGPT for a robust check.
It suggested the following:
os.getppidshould also be available on Windows. I am not so sure aboutps. Depending on the availability ofpson Windows, this check can also be used on Windows, otherwise only on unix systems.update: It is not available on Windows.
I think this is an improvement compared to the current heuristics.