subprocess.Popen(): OSError: [Errno 8] Exec format error in python?

PythonLinuxShell

Python Problem Overview


Yesterday, I wrote and ran a python script which executes a shell using subprocess.Popen(command.split()) where command is string which constitutes .sh script and its argument. This script was working fine until yesterday. Today, I ran the same script and now I am continuously hitting this error.

p=subprocess.Popen(shell_command.split())
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error

I know there are similar questions that have been asked before related to this question, but in my case I tried everything which doesn't solve my purpose. Using shell=True does not work because my shell script calls an another shell script before which some environment has to be set in order to run that script. I am badly stuck in this. I just restart my system once. I am using ubuntu 12.04

EDIT:

 import subprocess
 import os
 import sys

 arg1=sys.argv[1]
 arg2=sys.argve[2]

 shell_command = 'my_path/my_shell.sh ' + arg1 + ' '+ arg2
 P = subprocess.Popen(shell_command.split())
 P.wait()

my_shell.sh:

  arg1=$1
  arg2=$2

  cd $TOP
  setup the environment and run shell script
  build the kernel ...
  execute shell command .....

Python Solutions


Solution 1 - Python

I solved this by putting this line at the top of the called shell script:

#!/bin/sh

That will guarantee that the system always uses the correct interpreter when running your script.

Solution 2 - Python

Following statement worked for me

subprocess.Popen(['sh','my_script.sh'])

Solution 3 - Python

The error message suggests that the external program is not a valid executable.

Solution 4 - Python

As @tripleee said, there is an issue executing your script. Make sure:

  • Change the shell command to "./my_path/my_script.sh" or "/bin/bash my_path/my_script.sh". Account for environment variables, if necessary.
  • Both scripts have execute bit set (chmod +x)
  • The files exist at the location you think they do. (Use abspath or verify environment)
  • The files have contents
  • Try removing and re-typing the first line. I recommend killing the whole line, and hitting backspace several times in case there's a non-printable character before the #!

See also: https://stackoverflow.com/questions/1352922/why-is-usr-bin-env-python-supposedly-more-correct-than-just-usr-bin-pyt

Solution 5 - Python

This can also happen if the binary is not meant to run on your system.

I'm on OSX, but the binary I was running is not meant for OSX, as I saw from using file path/to/binary:

webui/bin/wkhtmltopdf: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.18, BuildID[sha1]=b6566a9e44c43a0eebf18d8c1dc6cb616801a77e, stripped

Solution 6 - Python

The error is because the executables are not given in the prescribed format for subprocess to execute it.

example:

shell_command1 = r"/path/to/executable/shell/file"

shell_command2 = r"./path/to/executable/shell/file"

subprocess.call(shell_command1) or subprocess.Popen(shell_command1) will not be able to run shell_command1 because subprocess needs an executable command like (mailx, ls, ping, etc.) or executable script like shell_command2 or you need to specify command like this

subprocess.call(["sh", shell_command1])
subprocess.Popen(["sh", shell_command1])

but however, you can use os.system() or os.Popen() as well

Solution 7 - Python

It is recommended to install the package binfmt-support to help the system better recognize the scipts. It helps regardless of whether they have a shebang line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmit SharmaView Question on Stackoverflow
Solution 1 - PythonWilliam PietriView Answer on Stackoverflow
Solution 2 - PythonneauView Answer on Stackoverflow
Solution 3 - PythontripleeeView Answer on Stackoverflow
Solution 4 - PythonCurtis MattoonView Answer on Stackoverflow
Solution 5 - PythonJoe FlackView Answer on Stackoverflow
Solution 6 - PythonDhinesh Sunder GanapathiView Answer on Stackoverflow
Solution 7 - PythonStanislav FyodorovView Answer on Stackoverflow