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

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 till yesterday. Today, I ran the same script and now i am contiguously hitting by 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 has been asked before related to this question. But, in my case i tried everything which doesn't solve my purpose. Using shell=True is not able to work because my shell script calls an another shell script before which some environment has to be set in order to ran 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

linux

shell

|

this question

edited Nov 7 '14 at 19:13 asked Nov 7 '14 at 18:42

Amit Sharma 913 1 6 18      Have you tried

import shlex and using

shlex.split(shell_command)? Docs:

docs.python.org/2/library/shlex.html –

Curtis Mattoon Nov 7 '14 at 18:52      Yes, it gives same error. :( –

Amit Sharma Nov 7 '14 at 18:56 1   Can you post your shell_command and the rest of the (relevant) code? Also, the shebang line of your script? –

Curtis Mattoon Nov 7 '14 at 19:00      @Curtis Matttoon ping.. –

Amit Sharma Nov 7 '14 at 19:19 1   What's the first line of your script? (#!/usr/bin/...?) Does the shell script actually exist in the path indicated? You might be better served with using absolute paths instead of 'my_path/my_shell.sh'. Ensure you have shebangs in both the python and shell script and they both have the execute bit set. If that still fails, you might have some strange whitespace or something before the 1st line. –

Curtis Mattoon Nov 7 '14 at 19:24

|

show more comments

5 Answers

5

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.

|

this answer answered May 30 '15 at 21:02

William Pietri 2,092 4 19 22 2   The solution that makes more sense to me, thanks! –

Pitto Jan 23 '16 at 17:49

|

Following statement worked for me

subprocess.Popen(['sh','my_script.sh'])ve a Python script that is running as a background process executing every 60 seconds. Part of that is a call to subprocess.Popen to get the output of ps. ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0] After

|

this answer answered Jan 5 '15 at 10:12

neau 117 8      The only caveat with this solution is that 'sh' should be in the path, which is not the case usually. –

nyuwec Apr 1 '16 at 12:42      this was preferable for me as I was working with a shell script I didn't want to alter. –

Harry Moreno Apr 24 at 18:46

|

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 #!

|

this answer

edited May 23 at 12:02

Community ♦ 1 1 answered Nov 7 '14 at 19:28

Curtis Mattoon 3,318 1 11 25

|

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

|

this answer answered Nov 7 '14 at 19:24

tripleee 65.2k 9 78 131

|

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.

|

this answer answered Apr 6 '16 at 15:11

Stanislav Fyodorov 15 5

|

ash shell. However when when I run the following snippet, I get : File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory import subprocesscommand_array = ['

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值