I am trying the absolute path of a given PPID from a shell script. However, the Parent Process may be any type of script (bash/csh/zsh/tsh/Makefile).
The child process is always a bash script and is the only script I have access on to edit.
I have tried:
ps --no-headers -o command $PPIDbut it only gets the command that invoked the parent process. This isn't what I need because the parent may have caused some cd's inside the script and I won't be able to resolve the relative path in the command to it.ls -l /proc/$PPID/fd/255and this was the closest to what I want but this is specific to bash scripts and as I mentioned, I don't have access to know my parent process' script type./proc/$PPID/exereturns the binary exe, and I need the script's absolute path that is using this binary.
There isn't such thing as "the" absolute path. There may be several way to access the same file. And some times, none of them (or more than one of them, depends how you look at it) are "the" main name. I am thinking of hard links here, for example.
Makefile is never the path of a script. It is just a configuration file. That is read by default by the executable. Which is probably
/usr/bin/make. To find a path to theMakefilefile, you need to read arguments of make. Or to find aMakefileamong open files (assuming there is only one, and that it is namedMakefile, and that is not certain). And the strategy to guess what is theMakefileof amakeprocess is specific tomake. You need another strategy to guess what is the script file executed by abashprocess, Another to find what is the python file executed by a python command (which could be a shebang python "executable", apython ../somerealtive/path/somefile.pyapython /some/absolute/pahtapython -m somemodele, apython -c "import somemod ; somemod.run()"etc.)Now, since you've mentioned
/proc/$PPID/exe, you can get the file "pointed" by this, usingpath=$(readlink -f /proc/$PPID/exe)Again, that is linux solution, more than a bash one. You have no guarantee from bash that the system on which you run this command have a
/procfilesystemI must add that I suspect a XY problem here. What are you trying to do exactly?