【问题标题】:Python subprocess returns non-zero exit status 1. Minimal working samplePython 子进程返回非零退出状态 1. 最小工作示例
【发布时间】:2019-01-23 19:45:56
【问题描述】:

为什么以管理员身份运行时这不起作用?

import subprocess

command = "wbadmin get versions"
output = subprocess.check_output(["powershell", command], timeout=120)
print(output)

当我从 powershell 或 cmd 运行它时,它可以工作。

当我使用 Popen 和communicate() 时出现错误:

"wbadmin" is not a known name, function or script..." 
The original message is in german:
b''
b'wbadmin : Die Benennung "wbadmin" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines \r\nausf\x81hrbaren Programms erkannt. \x9aberpr\x81fen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern \r\nenthalten), und wiederholen Sie den Vorgang.\r\nIn Zeile:1 Zeichen:1\r\n+ wbadmin get versions\r\n+ ~~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (wbadmin:String) [], CommandNotFoundException\r\n    + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\n'

【问题讨论】:

  • 您是否以管理员身份启动 Python(或您的脚本)?
  • 据我所知,是的。我首先以管理员身份运行 CMD,然后运行 ​​python。
  • cmd中运行命令(powershell wbadmin get versions)会输出什么?同时使用 Popencommunicate,并将输出粘贴到问题中。
  • "wbadmin 1.0 - Sicherungs-Befehlszeilentool (C) 版权所有 2013 Microsoft Corporation。Alle Rechte vorbehalten。FEHLER - Es wurde keine Sicherung gefunden。"当我在带有备份的服务器上运行它时,它会返回有关上次备份的信息
  • 不幸的是我也没有备份,所以我无法测试它。在有备份的服务器上,如果你从 Python 运行它,你会得到什么(仍然使用 Popencommunicate)?

标签: python winapi subprocess


【解决方案1】:

有问题的错误,cmets 倾向于指出这样一个事实:很大可能此处发布的代码不是您运行的代码(也许它已经过时了?)。

无论如何,运行您的 exact 代码(保存在名为 code.py 的文件中)会产生:

e:\Work\Dev\StackOverflow\q054334592>"e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" code.py
Traceback (most recent call last):
  File "code.py", line 4, in <module>
    output = subprocess.check_output(["powershell", command], timeout=120)
  File "c:\Install\x64\Python\Python\03.06.08\Lib\subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "c:\Install\x64\Python\Python\03.06.08\Lib\subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['powershell', 'wbadmin get versions']' returned non-zero exit status 1.

没有太多有用的信息。
check_output 只是一个方便的包装器,在当前情况下对我们不利。更多详情,请查看[Python 3]: subprocess - Subprocess management
所以,我修改了你的代码。

code.py

import subprocess

ps_args = "wbadmin get versions"

cmd = ["powershell", *ps_args.split(" ")]  # It works with your way too (["powershell", ps_args]), I'm just being rigorous
print("Popen arguments: {:}".format(cmd))

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate(timeout=120)

print("\nProcess terminated with exit code: {:}".format(proc.returncode))

print("\nSTDOUT:\n{:}\n".format(out.decode()))
print("STDERR:\n{:}\n".format(err.decode()))

这次的输出是:

e:\Work\Dev\StackOverflow\q054334592>"e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" code.py
Popen arguments: ['powershell', 'wbadmin', 'get', 'versions']

Process terminated with exit code: 1

STDOUT:
wbadmin 1.0 - Backup command-line tool
(C) Copyright Microsoft Corporation. All rights reserved.

ERROR - No backup was found.



STDERR:

现在,可以看到 PS (powershell) 命令运行尝试已成功运行,但命令本身失败。因此,subprocess(和 Python)方面的一切都很好(使用 check_output 时不太清楚的事实),问题是在 PS 方面。

就我而言,我没有备份,因此预计会失败。但无论如何,从这里看它更像是一个系统管理员任务。

【讨论】:

  • 非常感谢。我试过了,它给了我这个:wbadmin:“wbadmin”不能被识别为 cmdlet、函数或脚本......也许 PATH 变量有问题?
  • 这是一个 PS 错误。您可能没有安装该软件包。
  • 你这里的问题似乎不止一个。评论中的信息与问题中的信息不对应(突然““wbadmin”不能......”)。您正在不同的机器上尝试不同的结果,但由于外部原因。所以 Python 不是这里的问题,您可能应该将此问题标记为已解决并严格在 PS 上提交另一个问题.
【解决方案2】:

在命令中使用完整路径。例如:

ps_args = "/etc/bin/wbadmin get versions" 

【讨论】:

  • 不要认为引入 Nix 风格的路径就可以了。
猜你喜欢
  • 2020-10-24
  • 2015-03-11
  • 2014-09-23
  • 2012-01-17
  • 1970-01-01
  • 2012-04-21
  • 2014-01-13
  • 2019-10-02
  • 2017-03-19
相关资源
最近更新 更多