Python:你能说出调用shell的名字吗?

sta*_*tti 1 python shell

我有一个总是从shell调用的python脚本,可以是zsh或bash.

我怎么知道哪一个叫脚本?

Den*_*ach 7

在Linux中,您可以使用procfs:

>>> os.readlink('/proc/%d/exe' % os.getppid())
'/bin/bash'
Run Code Online (Sandbox Code Playgroud)

os.getppid()返回父进程的PID.这是便携式的.但是获取进程名称不能以便携方式完成.您可以解析ps所有unices上可用的输出,例如使用psutil.