How can I return control from my python script to the calling bash script?
I am using a bash script to loop through a configuration file and, based on that, call a python script via ssh. Unfortunately once the Python script does its job and I call quit
the bash script also gets clo开发者_开发问答sed, therefore the calling bash script's loop is terminated prematurely.
Here's my Bash Script
target | grep 'srv:' | while read l ; do srv $l $SSH ; done
srv () {
SSH=$2
SRV=`echo $1 | awk -F: '{print $2}'`
STATUS=`echo $1 | awk -F: '{print $3}'`
open $SSH "srv" $SRV $STATUS
}
then on the remote machine where the python script is called
if __name__== "main":
redirect('./Server.log', 'false')
conn()
if sys.argv[1] == "srv":
ServerState(sys.argv[2], sys.argv[3])
quit()
So looks like the quit() is also interrupting the script.
Nothing that the remote Python script does should be able to kill your do
loop unless you have done a set -e
in the local bash
first to make it sensitive to command failure — in which case it would die only if, as @EOL says, your remote script is hitting an exception and returning a nonzero/error value to SSH which will then die with a nonzero/error code locally.
What happens if you replace do srv
with do echo srv
so that you just get a printout of the commands you think you are running? Do you see several srv
command lines get printed out, and do they have the arguments you expect?
Oh: and, why are you using open
to allocate a new Linux virtual terminal for every single run of the command? You do not run out of virtual terminals that way?
It looks like quit()
is the function that makes your program stop.
If you can remove the call to quit()
(i.e. if it does nothing), just remove it.
Otherwise, you can run your Python program by hand (not from a shell script) and see what happens: it is likely that your Python script generates an exception, which makes the shell script stop. By running your Python program manually with realistic arguments (on the remote machine), you should be able to spot the problem.
You should be able to simply return from the main function.
精彩评论