29

Tried googling but couldn't find something that relates to my particular problem. I'm trying to run a shell script from python but the shell script wouldn't run because of a permission denied error. The python code I'm running is:

process = subprocess.Popen('run.sh', shell=True, stdout=subprocess.PIPE)
process.wait()
....
os.killpg(pro.pid, signal.SIGTERM)

The error I'm getting:

python RunScript.py "input"
/bin/sh: 1: run.sh: Permission denied

The contents of my shell script is:

#!/bin/sh
abspath=$(cd "$(dirname "$0")"; pwd)
CLASSPATH=$CLASSPATH:$abspath/"lib/*":$abspath/"bin"
export CLASSPATH
java -classpath $CLASSPATH my.folder.path.Class $abspath/../data/data.txt $abspath/../data/data2.txt

Thanks in advance.

Quanquan Liu
  • 1,427
  • 3
  • 16
  • 30
  • don't use `PIPE` unless you read/write from/to the pipe. It blocks subprocess if it generates enough output. [To discard output, you could redirect it to `os.devnull`](http://stackoverflow.com/q/11269575/4279) – jfs Nov 29 '13 at 18:41
  • you probably need [`preexec_fn=os.setsid` if you are going to use `os.killpg()`](http://stackoverflow.com/a/20184895/4279) – jfs Nov 29 '13 at 18:45

2 Answers2

36

Check your run.sh mode, if no executable flag, set it with command

chmod +x run.sh
PasteBT
  • 2,128
  • 16
  • 17
1

its because you don't have permission to run that script. You will need to give executable permission for that script to run.

chmod a+x run.sh

Arovit
  • 3,579
  • 5
  • 20
  • 24