In python: how can I get the exit status of a previous process with bash (ie "$?")?

I have a python script that should report success or failure of a previous command. I'm currently doing

command && myscript "Success" || myscript "Failed" 

I would like to do this in order to be able to run non-related commands:

 command; myscript 

And get myscript retrieve $? , i.e. existing status. I know I can run:

 command; myscript $? 

But what I'm really looking for is a python way to extract $? from python.

Any ideas? Thanks!

UPDATE: Since this is a strange request, let me clarify where it came from. I have a python script that uses the pushover API to send notifications to my phone. When I start a lengthy process, I start it as process && notify "success" || notify "failure" process && notify "success" || notify "failure" . But sometimes I forget to do it and just start the process. At this point, I would like to run "notify" the command line as before, as well as get the exit status and notify me. Of course, I could also implement a push api call in bash, but now the question is how to do this in python

+5
source share
4 answers

This may not be possible due to the way the script is executed (any language, not just Python). Try it: create a shell script called retcode.sh with the following contents:

 #!/bin/bash echo $? 

Make it executable, then try the following on the command line:

 foo # Or any other non-existent command echo $? # prints 127 foo ./retcode.sh # prints 0 

I would have to double check this, but it seems that all scripts, not just Python scripts, are run in a separate process that does not have access to the output code of the previous command executed by the "parent" shell process. What can explain why Python does not (as far as I can tell) give you a way to get the exit code of a previous command, for example bash $? , because there will always be 0, whatever.

I believe your approach to command; myscript $? execution command; myscript $? command; myscript $? is the best way to achieve what you are trying to do: let the Python script know about the exit code of the previous step.

Update:. After seeing your clarification about what you are trying to do, I think your best bet is to slightly modify your notify script so that it can have an option like -s or --status (using argparse , of course, to simplify the analysis options) and send a message based on the status code (“success” if the code was 0, “failed NN” if it was something else, where NN is the actual code). Then, when you type command without your little trick && notify "success" || notify "failure" && notify "success" || notify "failure" while it is still working, can you enter notify -s $? and get the notification you are looking for, as this will be the next thing that your shell starts after command returns.

+2
source
 false; export ret=$?; ./myscript2.py 

myscript2.py:

 #!/usr/bin/python import os print os.environ['ret'] 

Conclusion:

 1 
+2
source

But what I'm really looking for is a python way to extract $? from python.

If you run them as separate processes, then this is clearly impossible.

An independent process cannot know how another process ended.

You can 1) save the result in a file, 2) emit something from command and pass it to a script 3) run the command from myscript and so on ... but you need some kind of message.

The easiest way, of course, is command; myscript $? command; myscript $?

+1
source

Obviously not : the output value of a process is available only to its parent, and no shells that I know offer an API to let the next process get it.

IMHO, which is closer to your needs:

 process myscript $? 

That way, you can do this even if you started the process without thinking about the notification.

You can also make the script able to start the process, get the exit code and its notification, or (depending on the parameters specified on the command line) use the exit code specified as the parameter. For example, you could:

  • myscript process : starts the process and makes notifications
  • myscript -c $? : notifications only

argparse module can do this easily.

+1
source

Source: https://habr.com/ru/post/1205764/


All Articles