Running bash script from inside python

I have a problem with the following code:

callBash.py:

import subprocess print "start" subprocess.call("sleep.sh") print "end" 

sleep.sh:

 sleep 10 

I want the "end" to print in 10 seconds. (I know this is a stupid example, I could just sleep inside python, but this simple sleep.sh file was like a test)

+63
python bash call
Dec 06
source share
7 answers

Executing the sleep.sh executable and adding shell=True to the parameter list (as suggested in previous answers) works fine. Depending on your search path, you may also need to add ./ or another suitable path. (That is, change "sleep.sh" to "./sleep.sh" .)

The shell=True parameter is not needed (on a Posix system such as Linux) if the first line of the bash script is the path to the shell; e.g. #!/bin/bash .

+55
Dec 06
source share

In fact, you just need to add the shell=True argument:

 subprocess.call("sleep.sh", shell=True) 

But be careful -

Warning Calling the system shell with the shell = True can be a security risk in combination with untrusted input. See the Frequently Used Arguments section for more details.

source

+23
Dec 06
source share

If sleep.sh has shebang #!/bin/sh and has the appropriate file permissions - run chmod u+rx sleep.sh to make sure it is in $PATH , then your code should work as it is:

 import subprocess rc = subprocess.call("sleep.sh") 

If the script is not in the PATH, then specify the full path to it, for example, if it is in the current working directory:

 from subprocess import call rc = call("./sleep.sh") 

If the script does not have shebang, then you need to specify shell=True :

 rc = call("./sleep.sh", shell=True) 

If the script does not have executable permissions and you cannot change it, for example, by running os.chmod('sleep.sh', 0o755) , you can read the script as a text file and pass a line instead of subprocess :

 with open('sleep.sh', 'rb') as file: script = file.read() rc = call(script, shell=True) 
+20
Mar 14 '14 at 21:35
source share

Make sure sleep.sh has sleep.sh permissions and run it with shell=True :

 #!/usr/bin/python import subprocess print "start" subprocess.call("./sleep.sh", shell=True) print "end" 
+5
Dec 06
source share

If someone is looking for a script call with arguments

 import subprocess val = subprocess.check_call("./script.sh '%s'" % arg, shell=True) 

remember to convert the arguments to a string before passing, using str (arg).

This can be used to pass as many arguments as possible.

 subprocess.check_call("./script.ksh %s %s %s" % (agr1, str(arg2), arg3), shell=True) 
+2
Mar 12 '18 at 8:22
source share

Adding an answer because I was directed here after I asked how to run a bash script from python. You get an OSError: [Errno 2] file not found error OSError: [Errno 2] file not found if your script accepts parameters. Say, for example, your script took a sleep time parameter: subprocess.call("sleep.sh 10") will not work, you must pass it as an array: subprocess.call(["sleep.sh", 10])

+1
Nov 08 '16 at 16:04
source share

If chmod does not work, you are also trying

 import os os.system('sh script.sh') #you can also use bash instead of sh 

try me thanks

0
Apr 6 '18 at 5:40
source share



All Articles