OSError: [Errno 8] Exec 格式错误 [英] OSError: [Errno 8] Exec format error

查看:36
本文介绍了OSError: [Errno 8] Exec 格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解析 subprocess.Popen 的参数.我正在尝试在我的 Unix 服务器上执行一个脚本.在 shell 提示符下运行时的脚本语法如下:<代码>/usr/local/bin/script 主机名 = <主机名>-p LONGLIST.无论我如何尝试,脚本都没有在 subprocess.Popen

内运行

="前后的空格是必须的.

导入子流程Out = subprocess.Popen(['/usr/local/bin/script', 'hostname = ', '实际服务器名称', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr= 子进程.PIPE)

以上方法无效.

当我使用 shell=False 时,我得到 OSError: [Errno 8] Exec 格式错误

解决方案

OSError: [Errno 8] Exec format error 如果 shell 脚本顶部没有 shebang 行并且你正在尝试直接执行脚本.以下是重现该问题的示例:

<预><代码>>>>with open('a','w') as f: f.write('exit 0') # 创建脚本...>>>导入操作系统>>>os.chmod('a', 0b111101101) # rwxr-xr-x 使其可执行>>>os.execl('./a', './a') # 执行回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/usr/lib/python2.7/os.py",第 312 行,在 execl 中execv(文件,参数)OSError: [Errno 8] Exec 格式错误

要修复它,只需添加shebang,例如,如果它是一个shell脚本;在脚本顶部添加 #!/bin/sh:

<预><代码>>>>with open('a','w') as f: f.write('#!/bin/sh\nexit 0')...>>>os.execl('./a', './a')

它执行 exit 0 没有任何错误.

<小时>

在 POSIX 系统上,shell 解析命令行,即,您的脚本不会看到 = 周围的空格,例如,如果 script 是:

#!/usr/bin/env python导入系统打印(sys.argv)

然后在shell中运行它:

$/usr/local/bin/script 主机名 = '<主机名>'-p 长列表

产生:

['/usr/local/bin/script', 'hostname', '=', '', '-p', 'LONGLIST']

注意:'=' 周围没有空格.我在 周围添加了引号以转义重定向元字符 <>.

要在 Python 中模拟 shell 命令,请运行:

from subprocess import check_callcmd = ['/usr/local/bin/script', 'hostname', '=', '', '-p', 'LONGLIST']check_call(cmd)

注意:没有shell=True.并且您不需要转义 <> 因为没有运行 shell.

"Exec format error" 可能表示您的 script 格式无效,请运行:

$ 文件/usr/local/bin/script

找出它是什么.将架构与以下输出进行比较:

$ uname -m

I am having hard time parsing the arguments to subprocess.Popen. I am trying to execute a script on my Unix server. The script syntax when running on shell prompt is as follows: /usr/local/bin/script hostname = <hostname> -p LONGLIST. No matter how I try, the script is not running inside subprocess.Popen

The space before and after "=" is mandatory.

import subprocess
Out = subprocess.Popen(['/usr/local/bin/script', 'hostname = ', 'actual server name', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

The above does not work.

And when I use shell=False, I get OSError: [Errno 8] Exec format error

解决方案

OSError: [Errno 8] Exec format error can happen if there is no shebang line at the top of the shell script and you are trying to execute the script directly. Here's an example that reproduces the issue:

>>> with open('a','w') as f: f.write('exit 0') # create the script
... 
>>> import os
>>> os.chmod('a', 0b111101101) # rwxr-xr-x make it executable                       
>>> os.execl('./a', './a')     # execute it                                            
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/os.py", line 312, in execl
    execv(file, args)
OSError: [Errno 8] Exec format error

To fix it, just add the shebang e.g., if it is a shell script; prepend #!/bin/sh at the top of your script:

>>> with open('a','w') as f: f.write('#!/bin/sh\nexit 0')
... 
>>> os.execl('./a', './a')

It executes exit 0 without any errors.


On POSIX systems, shell parses the command line i.e., your script won't see spaces around = e.g., if script is:

#!/usr/bin/env python
import sys
print(sys.argv)

then running it in the shell:

$ /usr/local/bin/script hostname = '<hostname>' -p LONGLIST

produces:

['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']

Note: no spaces around '='. I've added quotes around <hostname> to escape the redirection metacharacters <>.

To emulate the shell command in Python, run:

from subprocess import check_call

cmd = ['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']
check_call(cmd)

Note: no shell=True. And you don't need to escape <> because no shell is run.

"Exec format error" might indicate that your script has invalid format, run:

$ file /usr/local/bin/script

to find out what it is. Compare the architecture with the output of:

$ uname -m

这篇关于OSError: [Errno 8] Exec 格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆