Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit a python file to only be run by a bash script

Tags:

python

bash

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.

like image 816
sshays68 Avatar asked Feb 07 '26 06:02

sshays68


2 Answers

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.

like image 163
Mikko Ohtamaa Avatar answered Feb 09 '26 12:02

Mikko Ohtamaa


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
like image 43
Klaus D. Avatar answered Feb 09 '26 10:02

Klaus D.