I am trying to launch a .sh script from Python 3.3 in Ubuntu 13.10. The script is supposed to shutdown the computer. I have already marked the sh script as executable through the terminal. I have tried to run the sh script through: os.system("script.sh"), subprocess.Popen("Script.sh"), and subprocess.call([script.sh]). They keep returning the: OSError Exec format error. Any help would be greatly appriciated!
-
Have you added a shebang to the script? Otherwise the OS tries to run it as a binary executable.Kritzefitz– Kritzefitz2014-01-16 21:04:40 +00:00Commented Jan 16, 2014 at 21:04
-
Hey. What do you mean by 'shebang'? I am able to run the script from the terminal, but not from python.Luke Dinkler– Luke Dinkler2014-01-16 23:47:23 +00:00Commented Jan 16, 2014 at 23:47
-
On unix-like systems scripts (executables that aren't binary code) need to have an information how they should be executed. So the first line of the .sh scripts needs to be a so called shebang. A shebang has the form "#!/your/script/interpreter" (without quotation marks". For sh scripts /your/script/interpreter is /bin/sh. For a detailed description of shebangs look at en.wikipedia.org/wiki/Shebang_%28Unix%29.Kritzefitz– Kritzefitz2014-01-17 14:18:42 +00:00Commented Jan 17, 2014 at 14:18
Add a comment
|
1 Answer
I assume that script.sh isn't in your PATH but in your current working directory. By default os.system and subprocess look in your path for the requested executable. So to execute something in your current working directory you need to specify the executable like this:
subprocess.call("./script.sh")
The ./simply says that the executable that should be executed is in the current working directory.