1

I'm trying to invoke a shell script shell_script.sh from a python script (python_script.py) using the call command. The shell_script.sh invokes a executable that requires root access to execute.

The python_script.py invokes shell_script.sh using subprocess.call().

See below: subprocess.call(['/complete_path/shell_script.sh', 'param1', 'param2', 'param3'], shell=True)

When I try to execute the python script python_script.py it gives me permission denied.

I've tried different ways.

a) Invoke python with sudo - sudo python python_script.py

b) Invoke sudo into inside the call method - subprocess.call(['sudo' '/complete_path/shell_script.sh', 'param1', 'param2', 'param3'], shell=True)

What's the best way to resolve this.

Thanks.

1 Answer 1

1

I'd put logic in the python_script.py to check its UID and fail if is not executed as root. if os.getuid() != 0:. That will ensure it only runs as root, ether by a root login, or sudo.

If you're getting permission denied when trying to execute the python_script.py, you need to set the execute bit on it. chmod +x python_script.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.