I am looking for a way to limit how a python file to be called. Basically I only want it to be executable when I call it from a bash script but if ran directly either from a terminal or any other way I do not want it to be able to run. I am not sure if there is a way to do this or not but I figured I would give it a shot.
There is no meaningful way to do this.
UNIX process architecture does not work this way. You cannot control the execution of a script by its parent process.
Instead we should discuss why you want to do something like this, you are probably thinking doing it in a wrong way and what good options there is to address the actual underlying problem.
You could run it from the bash script with a special environment variable set, like:
FROM_BASH=1 /path/to/your_python_script.py
Then you check if the variable is set:
if os.environ.get('FROM_BASH'):
# your exit code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With