Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the name of the calling shell in Python?

Tags:

python

shell

I have a Python script that is always called from a shell, which can be either zsh or bash.

How can I tell which one called the script?

like image 493
static_rtti Avatar asked Oct 27 '25 09:10

static_rtti


1 Answers

In Linux you can use procfs:

>>> os.readlink('/proc/%d/exe' % os.getppid())
'/bin/bash'

os.getppid() returns the PID of parent process. This is portable. But obtaining process name can't be done in portable way. You can parse ps output which is available on all unices, e.g. with psutil.

like image 190
Denis Otkidach Avatar answered Oct 29 '25 23:10

Denis Otkidach