I have a script reading an identifier
ID=$(GPU.py --get)
#Do something
GPU.py --release $ID
Now from GPU.py
I want to test what was the PID of the --get
calling process so I call
os.getppid()
. On release I can then test if the given ID
was released by the same process.
~~In case of the Bash script above, it does not work, while it calls subshell.~~
Actually it does! I was confused but as accepted answer points out there is some optimization not running subshell! The only problem was I have rewritten it as below and it stopped working.
I was thinking I will be lucky doing
read ID < <(GPU.py --get)
But it seems also here subshell is created and os.getppid()
does not return PID of the script. Could you help me to fix it? And the fix is not to call os.getpppid()
while I can call the Python program not only from Bash.
When command substitution runs only a single command, which is your case, then bash
optimizes by running directly the command without creating a subshell.
You can verify with the following :
echo $$ $(python -c 'import os; print(os.getppid())')