Send correct exit code from child script back to parent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send correct exit code from child script back to parent
# 1  
Old 05-16-2012
Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue:

I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh

Inside this script I have this piece of code:
Code:
if [ "$flag" = "yes" ]
then
        /usr/local/bin/ssh $sftpserver mv $sftppath/$filenameshort $finaldirname  >> ${_ftpdebug} 2>&1
        ssh faunus:/home/dslmain/scripts/senddcmcadefttest.sh
        if [ $? -ne 0 ]
        then
           echo "EXIT: " $?
           exit
        fi
        echo "DONE"
else
        echo "FAILED"
fi

I call another script via ssh (red text).

This is the contents of the script: senddcmcadefttest.sh
Code:
#!/bin/sh -x
node=fid
NODE=FAUNUS
_Ymd=`date +%Y%m%d`
indir=/home/dslmain/export/${node}/eft/in
sentdir=/home/dslmain/export/${node}/eft/sent
LOG="/home/dslmain/export/$node/eft/logs/eft.log.`date +%m%d`"
_temp=/utemp/tmp_logs/eftexists
if [ -s $_temp ]
then
  rm $_temp
fi
_ym=`date +%m%d`

ls ${indir}/*${_ym}* > $_temp
if [ ! -s $_temp ]
then
   echo "No EFT files detected" >> ${LOG}
   exit 1
fi
cd ${indir}
for FILE in `cat $_temp|awk -F'/' '{print $NF}'`
do
  if [ -s ${FILE} ]
  then
    echo "Here _UPLOAD function would have run"
    echo "Snapshot of files of the day found on RBC FTP" >> ${LOG}
    A=1
    B=1
    echo $A >> ${LOG}
    echo $B >> ${LOG} 
    if [ ${A} -eq ${B} ]
    then
      echo "${FILE} was successfully uploaded to RBC" >> ${LOG}
      echo ""  >> ${LOG}
      mv ${indir}/${FILE} ${sentdir}/${FILE}
      exit 0
    else
      echo "${indir}/${FILE} failed to be loaded to RBC" >> ${LOG}
      echo " "  >> ${LOG}
      exit 2
    fi
  else
    echo "Sorry file dose not exist, or file is empty" >> ${LOG}
    exit 3
  fi
done

So in the child script I have 3 different scenarios (in red) where the exit code will be either 0, 2 or 3...I want to pass the correct exit code back to the parent script
copylsofdcmcadefttosftpwithmove.sh so that it can be evaluated in the "if" statement...however $? is always coming back as "0"...

Thanks for reading and any assistance.

Regards
Giuliano

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by gvolpini; 05-16-2012 at 11:39 AM..
# 2  
Old 05-16-2012
You are getting the return code from ssh, not your script.
# 3  
Old 05-16-2012
In the main script can you try this :

instead

ssh faunus:/home/dslmain/scripts/senddcmcadefttest.sh
this :

ssh user@remote_machine "/home/dslmain/scripts/senddcmcadefttest.sh ; exit \$?"

Basically this way I make sure that the ssh ends with same exit status as the remotely launched script . Hope the ssh method suites you, is the way I used it and worked for me.

---------- Post updated at 09:04 AM ---------- Previous update was at 08:54 AM ----------

If this doesn't work I'd also suggest a basic check: launching the remote script directly, on the remote machine and make sure that it returns the desired exit status codes. To make sure that the exit status is ok but you;re just not able to capture it and eliminate the possibility that the exit status is wrong ( "0" ) and you';re capturing it right.
This User Gave Thanks to black_fender For This Post:
# 4  
Old 05-16-2012
Thanks black_fender worked splendidly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fail Parent Script if any of the child fails

I have requirement where I need to fail parent if any one of the child process fails. Here is the code snippet for i in 1 2 3 4 5 6 7 8 9 10 do child_script $i & done wait I need to fail my main script if any one of my child process fails (8 Replies)
Discussion started by: gvkumar25
8 Replies

2. Shell Programming and Scripting

How to exit from the parent script while the child is running?

hi, i want to call a child shell script from a parent shell script. the child will be running for 5 mins. normally when the child is running, parent will wait till the child completes. so in the above case parent will be paused for 5 mins. is there a way so that the parents does not wait for the... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

4. Shell Programming and Scripting

Child exiting and not returning to parent script

I am having a parent scripts which reads a file with child scripts name. I need to read one by one child script , execute it and 1. If child script fails send mail to the team with the log file 2. If the child script executes fine then proceed with the next child script execution. #!... (3 Replies)
Discussion started by: nw2unx123
3 Replies

5. UNIX for Dummies Questions & Answers

Why is the return code of child required by parent ?

Hello everyone, I am a complete newbie to UNIX. I am using Debian LXDE 64-bit. I have a question regarding the child and parent process communication. According to wikipedia.org and various other sources, when a child process exits it sends the SIGCHLD signal to its parent... (1 Reply)
Discussion started by: sreyan32
1 Replies

6. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

7. Shell Programming and Scripting

Script, child kills parent

Hello everyone, I'm trying to write a script, i would like to say the child kills the parent, how would i do that? (2 Replies)
Discussion started by: jessy21
2 Replies

8. Shell Programming and Scripting

How to return control from the child script to the parent one?

I have two shell scripts : A.sh and B.sh A.sh echo "In A" exec B.sh echo "After B" B.sh echo "In B" The output is : In A In B I want the output : In A In B After B (4 Replies)
Discussion started by: suchismitasuchi
4 Replies

9. Shell Programming and Scripting

Passing a variable from a child script back to the parent

Hi I have written a script using ftp to get files from one server and copy them to 2 dirrerent servers. I wish to call this script from a parent script that will check the number of files copied and run a check sum for each file. As the filenames for the files in the get portion of the script... (3 Replies)
Discussion started by: Andy82
3 Replies

10. Shell Programming and Scripting

Parent/child Korn shell script help

I have the following two Korn shell scripts: SHELL1.ksh #!/usr/bin/ksh nohup sas /abc/123/sasprogram1.sas & SHELL2.ksh #!/usr/bin/ksh ./SHELL1.ksh wait nohup sas /abc/123/sasprogram2.sas & My goal is to run SHELL1.ksh within SHELL2.ksh. SHELL1.ksh runs sasprogram1.sas. I would like... (1 Reply)
Discussion started by: sasaliasim
1 Replies
Login or Register to Ask a Question