在 python 中:如何获取从 bash 运行的前一个进程的退出状态(即“$?”)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26626713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In python: how can I get the exit status of the previous process run from bash (i.e. "$?")?
提问by Miquel
I have a python script that should report success or failure of the previous command. Currently, I'm doing
我有一个 python 脚本,它应该报告上一个命令的成功或失败。目前,我正在做
command && myscript "Success" || myscript "Failed"
What I would like to do is instead to be able to run the commands unlinked as in:
我想做的是能够运行未链接的命令,如下所示:
command; myscript
And have myscript
retrieve $?
, i.e. the exist status. I know I could run:
并具有myscript
检索$?
,即存在状态。我知道我可以运行:
command; myscript $?
But what I'm really looking for is a python way to retrieve $?
from python.
但我真正在寻找的是一种$?
从 python 中检索的 python 方式。
Any ideas? Thanks!
有任何想法吗?谢谢!
UPDATE:
Since this is a strange request, let me clarify where it comes from.
I have a python script that uses the pushoverAPI to send a notification to my phone.
When I run a long process, I run it as process && notify "success" || notify "failure"
. But some times I forget to do this and just run the process. At this point, I'd like to run "notify" on the still processing command line, and have it pick up the exit status and notify me.
Of course I could also implement the pushover api call in bash, but now it's become a question of figuring out how to do it in python
更新:由于这是一个奇怪的请求,让我澄清它的来源。我有一个 python 脚本,它使用pushoverAPI 向我的手机发送通知。当我运行一个很长的进程时,我将它作为process && notify "success" || notify "failure"
. 但有时我会忘记这样做而只是运行该过程。此时,我想在仍在处理的命令行上运行“notify”,让它获取退出状态并通知我。当然我也可以在 bash 中实现 pushover api 调用,但现在它变成了弄清楚如何在 python 中执行的问题
采纳答案by rmunn
This may not be possible, because of how a script (of any language, not just Python) gets executed. Try this: create a shell script called retcode.sh
with the following contents:
这可能是不可能的,因为脚本(任何语言,而不仅仅是 Python)是如何执行的。试试这个:创建一个retcode.sh
使用以下内容调用的 shell 脚本:
#!/bin/bash
echo $?
Make it executable, then try the following at a shell prompt:
使其可执行,然后在 shell 提示下尝试以下操作:
foo # Or any other non-existent command
echo $? # prints 127
foo
./retcode.sh # prints 0
I'd have to double-check this, but it seems that allscripts, not just Python scripts, are run in a separate process that doesn't have access to the exit code of the previous command run by the "parent" shell process. Which may explain why Python doesn't (as far as I can tell) give you a way to retrieve the exit code of the previous command like bash's $?
— because it would always be 0, no matter what.
我必须仔细检查这一点,但似乎所有脚本,而不仅仅是 Python 脚本,都在一个单独的进程中运行,该进程无权访问“父”shell 进程运行的前一个命令的退出代码. 这可以解释为什么 Python 没有(据我所知)给你一种方法来检索前一个命令的退出代码,就像 bash 的一样$?
——因为无论如何它总是 0。
I believe your approach of doing command; myscript $?
is the best way to achieve what you're trying to do: let the Python script know about the exit code of the previous step.
我相信您的做法command; myscript $?
是实现您想要做的事情的最佳方式:让 Python 脚本知道上一步的退出代码。
Update:After seeing your clarification of what you're trying to do, I think your best bet will be to modify your notify
script just a little, so that it can take an option like -s
or --status
(using argparseto make parsing options easier, of course) and send a message based on the status code ("success" if the code was 0, "failure NN" if it was anything else, where NN is the actual code). Then when you type command
without your little && notify "success" || notify "failure"
trick, while it's still running you can type notify -s $?
and get the notification you're looking for, since that will be the next thing that your shell runs after command
returns.
更新:在看到您对您正在尝试做的事情的澄清后,我认为您最好的办法notify
是稍微修改您的脚本,以便它可以选择像-s
或这样的选项--status
(当然,使用argparse使解析选项更容易) ) 并根据状态代码发送消息(如果代码为 0,则为“成功”,如果是其他任何内容,则为“失败 NN”,其中 NN 是实际代码)。然后,当您在command
没有小&& notify "success" || notify "failure"
技巧的情况下键入时,当它仍在运行时,您可以键入notify -s $?
并获取您正在寻找的通知,因为这将是您的 shell 在command
返回后运行的下一件事情。
回答by Cyrus
false; export ret=$?; ./myscript2.py
myscript2.py:
myscript2.py:
#!/usr/bin/python
import os
print os.environ['ret']
Output:
输出:
1
回答by Karoly Horvath
But what I'm really looking for is a python way to retrieve $? from python.
但我真正在寻找的是一种检索 $? 来自蟒蛇。
If you ran them as separateprocesses, then it's clearly impossible.
如果您将它们作为单独的进程运行,那么这显然是不可能的。
An independent process cannot know how another process ended.
一个独立的进程无法知道另一个进程是如何结束的。
You can 1) store the result in a file, 2) emit something from command
, and pipe it to the script 3) run the command from myscript
, and so on... but you need some kind of communication.
您可以 1) 将结果存储在一个文件中,2) 从 发出一些东西command
,并将其通过管道传输到脚本 3) 从 运行命令myscript
,依此类推……但您需要某种通信。
The simplest way, of course is command; myscript $?
最简单的方法当然是 command; myscript $?
回答by Serge Ballesta
It is clearly notpossible : the exit value of a process is only accessible to its parent, and no shells I know offer an API to allow next process to retrieve it.
这显然是没有可能的:一个进程的出口值只有其父访问,没有炮弹,我知道提供了一个API,允许下道工序进行检索。
IMHO what is closer to your need would be :
恕我直言,更接近您的需求的是:
process
myscript $?
That way, you can do it even if you started you process without thinking about notification.
这样,即使您开始处理而不考虑通知,您也可以这样做。
You could also make the script able to run a process get the exit code and to its notification, or (depending on options given in command line) use an exit code given as parameter. For example you could have :
您还可以使脚本能够运行一个进程,获取退出代码和通知,或者(取决于命令行中给出的选项)使用作为参数给出的退出代码。例如你可以有:
myscript process
: runsprocess
and does notificationsmyscript -c $?
: only does notifications
myscript process
: 运行process
并执行通知myscript -c $?
: 只做通知
argparse
module can make that easy.
argparse
模块可以使这变得容易。