Skip to main content
added 461 characters in body
Source Link

Outside of Linux land or lacking access to the /proc filesystem or equivelent, you can make use of pstree:

Assuming you have the pid of

On a Mac:

./test.sh 
16012
-+= 00001 root /sbin/launchd
 \-+= 00245 wingwong /sbin/launchd
   \-+= 04670 wingwong /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_2052597
     \-+= 11816 root login -pf wingwong
       \-+= 11817 wingwong -bash
         \-+= 16012 wingwong ksh ./test.sh
           \-+- 16013 wingwong pstree -p 16012

On a Linux box:

./test.sh 
14981
bash(14981)---pstree(14982)

The format and style of the output from pstree differs, depending on your environment, but you can enforce ASCII output and then sed/tr/awk/etc. filter the output to get the shell that is running the script.

So a cleaned up output version(works for Mac or Linux OS runs):

#!/usr/bin/env sh
pstree  -p $$  | tr ' ()' '\012\012\012' | grep -i "sh$" | grep -v "$0" | tail -1

On run yields:

./test.sh 
sh

And when run with a different shell:

#!/usr/bin/env ksh
pstree  -p $$  | tr ' ()' '\012\012\012' | grep -i "sh$" | grep -v "$0" | tail -1

Yields:

./test.sh 
ksh

No root or special filesystem required. Note, my filtering assumes that the shell binary name ends with sh and that there are no intermediate entries which end with sh. Also assumes that you didn't name your script "sh" or some unfortunate grep pattern that will obliterate information. :) Will require some customization for your own environment to ensure a higher degree of foolproofing.

Outside of Linux land or lacking access to the /proc filesystem or equivelent, you can make use of pstree:

Assuming you have the pid of

On a Mac:

./test.sh 
16012
-+= 00001 root /sbin/launchd
 \-+= 00245 wingwong /sbin/launchd
   \-+= 04670 wingwong /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_2052597
     \-+= 11816 root login -pf wingwong
       \-+= 11817 wingwong -bash
         \-+= 16012 wingwong ksh ./test.sh
           \-+- 16013 wingwong pstree -p 16012

On a Linux box:

./test.sh 
14981
bash(14981)---pstree(14982)

The format and style of the output from pstree differs, depending on your environment, but you can enforce ASCII output and then sed/tr/awk/etc. filter the output to get the shell that is running the script.

Outside of Linux land or lacking access to the /proc filesystem or equivelent, you can make use of pstree:

Assuming you have the pid of

On a Mac:

./test.sh 
16012
-+= 00001 root /sbin/launchd
 \-+= 00245 wingwong /sbin/launchd
   \-+= 04670 wingwong /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_2052597
     \-+= 11816 root login -pf wingwong
       \-+= 11817 wingwong -bash
         \-+= 16012 wingwong ksh ./test.sh
           \-+- 16013 wingwong pstree -p 16012

On a Linux box:

./test.sh 
14981
bash(14981)---pstree(14982)

The format and style of the output from pstree differs, depending on your environment, but you can enforce ASCII output and then sed/tr/awk/etc. filter the output to get the shell that is running the script.

So a cleaned up output version(works for Mac or Linux OS runs):

#!/usr/bin/env sh
pstree  -p $$  | tr ' ()' '\012\012\012' | grep -i "sh$" | grep -v "$0" | tail -1

On run yields:

./test.sh 
sh

And when run with a different shell:

#!/usr/bin/env ksh
pstree  -p $$  | tr ' ()' '\012\012\012' | grep -i "sh$" | grep -v "$0" | tail -1

Yields:

./test.sh 
ksh

No root or special filesystem required. Note, my filtering assumes that the shell binary name ends with sh and that there are no intermediate entries which end with sh. Also assumes that you didn't name your script "sh" or some unfortunate grep pattern that will obliterate information. :) Will require some customization for your own environment to ensure a higher degree of foolproofing.

Source Link

Outside of Linux land or lacking access to the /proc filesystem or equivelent, you can make use of pstree:

Assuming you have the pid of

On a Mac:

./test.sh 
16012
-+= 00001 root /sbin/launchd
 \-+= 00245 wingwong /sbin/launchd
   \-+= 04670 wingwong /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_2052597
     \-+= 11816 root login -pf wingwong
       \-+= 11817 wingwong -bash
         \-+= 16012 wingwong ksh ./test.sh
           \-+- 16013 wingwong pstree -p 16012

On a Linux box:

./test.sh 
14981
bash(14981)---pstree(14982)

The format and style of the output from pstree differs, depending on your environment, but you can enforce ASCII output and then sed/tr/awk/etc. filter the output to get the shell that is running the script.