Issue
I am building a custom fetch tool to add as a feature for my hobby OS. I wanted to make the script in pure Python.
How can I detect which shell invoked the script? This is a difficult problem because I am calling a script in a script to detect the shell. For example, Python code like os.getenv("SHELL")
results in /bin/bash
no matter what.
I also tried these command-line options:
getent passwd USERID
--> Always printsBash
echo $0
--> prints/bin/sh
readlink /proc/$$/exe
--> Always printsReadlink
P.S. : I am not running a new shell, I am just printing out the name of the shell that invoked this script, It's all for the sake of an aesthetically pleasing look, with some system information.
Solution
On Linux, to detect the parent process of your Python interpreter, you can use:
os.readlink(f'/proc/{os.getppid()}/exe')
If your Python interpreter was directly invoked by a shell (which is not guaranteed!) via the regular fork/exec process (not a direct exec
replacement-in-place -- again, not guaranteed!), you'll see it returned there.
Answered By - Charles Duffy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.