0

I am using Popen to remote call script

s = Popen(['ssh' , ssh_argument0 , ssh_argument1 , '/tmp/remote/dude_rc_admsvr.sh %s %s' %(DomainHome , ACTIVITY)]) 

stdout = s.communicate()

print stdout

The script is not exiting with the status mentioned under shell script , instead it only prints success or failure status only ..

i want to exit with the status codes as per shell script. here is shell script

tail -Fn0 ${ADM_DOMAIN_LOG} | \
while read LOG_LINE;
do
 echo ${LOG_LINE} | grep -q "${PASS_MSG}"
 if [ $? = 0 ]
 then
   echo "${STATUS_SUCCESS}"
   exit 0
 elif echo ${LOG_LINE} | grep -q "${FAIL_MSG}"
 then
   echo "${STATUS_FAILURE}"
   exit 1
 elif echo  ${LOG_LINE} | grep -q "${FAIL_MSG2}"
 then
    echo "${STATUS_FAILURE}"
    exit 1
 fi
done
exit 

How to get the status code returned ?

3 Answers3

0

From subprocess.check_call's documentation:

If the return code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute.

Therefore, to get the status code, you need to catch the exception and retrieve the returncode attribute.

try:
    check_call(['ssh' , ssh_argument0 , ssh_argument1 , '/tmp/remote/dude_rc_admsvr.sh %s %s' %(DomainHome , ACTIVITY)]) 
except subprocess.CalledProcessError as e:
    print('status code = %s' % e.returncode)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • But why its not taking the exit code from shell script , i have already added exit 0 , exit 1 to shell code – user2900860 Jun 27 '18 at 05:54
  • Yeah , Same issue its not exiting + echo '' '' '' '' '' + grep -q 'Server state changed to RUNNING' + [ 0 = 0 ] + echo P + exit 0 – user2900860 Jun 27 '18 at 06:18
  • @user2900860 In what you just posted, the script appears to exit with 0, meaning success, so naturally `check_call` returns without raising an exception, and therefore won't go into the except block to print the status code. Did you try a scenario where the script returns 1 instead? – blhsing Jun 27 '18 at 06:27
  • Its not even exiting with exit 1 grep -q 'Server state changed to RUNNING' + [ 0 = 0 ] + echo P + exit 1 – user2900860 Jun 27 '18 at 06:36
  • What do you mean by "it's not even exiting"? Is the script not finishing? Did you make sure the script runs with the given arguments in command line first? Did you try using ssh to run the script in command line first? – blhsing Jun 27 '18 at 06:44
  • Yeah i tried in the local unix box its exiting but via Popen(ssh) its not exiting .. – user2900860 Jun 27 '18 at 06:45
  • s = Popen(['ssh' , ssh_argument0 , ssh_argument1 , '/tmp/remote/dude_rc_admsvr.sh %s %s' %(DomainHome , ACTIVITY)]) stdout = s.communicate() print stdout – user2900860 Jun 27 '18 at 07:04
  • But you question does not include `Popen` or `communicate()`. Please update your question with your actual code. – blhsing Jun 27 '18 at 07:06
0

Adding a new answer since you changed your code in question from using check_call to using Popen and communicate.

From subprocess.communicate's documentation:

Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

In other words, you need to add stdout=PIPE to your call to Popen's constructor in order to use communicate.

s = Popen(['ssh' , ssh_argument0 , ssh_argument1 , '/tmp/remote/dude_rc_admsvr.sh %s %s' %(DomainHome , ACTIVITY)], stdout=PIPE)
stdout = s.communicate()
returncode = s.returncode
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • the shell script is not exiting from popen ssh code , i dont care about the return code i just want to exit from the console/function – user2900860 Jun 28 '18 at 09:44
  • @user2900860 Try adding `input=PIPE` to the arguments as well. By the way you did stress that you want get the status code in your question (you said "How to get the status code returned ?"). Saying that you don't care about it now just contradicts yourself. – blhsing Jun 28 '18 at 15:13
  • The script is not exiting with the status mentioned under shell script , instead it "only prints success or failure status only ." thats what i mentioned above i need shell script exit status instead of popen .. – user2900860 Jun 28 '18 at 15:48
  • @user2900860 Did adding `input=PIPE` help? – blhsing Jun 28 '18 at 15:49
  • resolved tail was holding ssh session which was reolved by adding kill -9 |`ps -eaf |grep tail`.. – user2900860 Jun 28 '18 at 16:07
0

Found Issue :

The tail statement in shell script was holding the SSH session which is why Popen couldn't able to exit the subshell , Adding kill -9 ps -eaf |grep tail resolved the issue.