Home > front end >  Permission denied to run shell script by subprocess.call
Permission denied to run shell script by subprocess.call

Time:07-02

I wrote a shell script using python and it successfully run by command bash download_R1.sh on my terminal.

I wanted to test if subprocess can do the same thing in python because I would like to integrate a pipeline in python script. Here is the subprocess part of my code:

downR1 = subprocess.call('./download_R1.sh')

It failed to run the script with following error message:

Error message:
PermissionError: [Errno 13] Permission denied: './download_R1.sh'

Someone suggested using chmod x to obtain authenitification of that script. But bash down.sh is working. I don't know which part goes wrong.

Any advice?

CodePudding user response:

That's the normal way the shell behaves.

You can not directly run a program or script if it not has execution privileges ( x)

But, you can instruct other program bash, python or whatever to run a file even if not has x

foo.sh

#!/bin/bash
echo "Hello World"

foo.py

#!/usr/bin/env python
print "Hello World"
$ bash foo.sh
Hello World
$ ./foo.sh
bash: ./foo.sh: Permission denied

$ python foo.py
Hello World


$ ./foo.py
bash: ./foo.sh: Permission denied

$ chmod  x foo.sh foo.py

$ ./foo.sh
Hello World
$ ./foo.py
Hello World

In your case, if you want to run the program with subprocess but not giving execution privileges to the file you must use:

subprocess.run(["python", "download_R1.sh"])
  • Related