11 Replies Latest reply on Aug 24, 2005 9:14 AM by Tim Fessenden

    Feeding return code back from runscript

      Share This:

      When I call an NSH script, using runscript within another NSH script, the return code is not fed back.


      Here's an example of code:


      runscript -h $1 -p 50 -e /usr/nsh/storage/scripts/checkNetHSM.nsh
      echo $?

      $? always returns 0, although the NSH script called returns 1 using similar syntax when calling a KSH script:

      nexec -e /opt/unixinf/appcontrol/bin/controlNetHsm.ksh
      exit $RC

      This is an operators job. One NSH script would do it, but the target list must be cleared after the job is executed, else the operators might run the job on the wrong target list.


      My question is about getting the return code from runscript, but suggestions on running another way to do it are also welcome :)

        • 1. Re: Feeding return code back from runscript

          Looks like a bug based on the man page for runscrit:


          EXIT STATUS
          If the given command or script is successfully executed on all named
          hosts then these programs will exit with a status of 0. If an error
          occurs or if and given command or script exits with a non zero status,
          then these programs will exit with a non-zero status.


          A quick workaround would be to have the script being called (checkNetHSM) echo the error code and have the runscript interpret the return code.

          • 2. Re: Feeding return code back from runscript

            What version of the client are you using? Is it 6.3.1?

            • 3. Re: Feeding return code back from runscript

              Everything is


              Can you suggest how we get the suggested workaround to work?


              Note we are calling a type1 script from a type2 script.

              This is because we need to run a single blcli command immediately after running a korn shell script on multiple agents. If we put them both in a type1 script, then the blcli command runs on each agent and takes the job from 10 seconds to around 6 minutes.

              • 4. Re: Feeding return code back from runscript

                Here is a quick work-around.


                In the script being called, (checkNetHSM.nsh) put in an echo statement right before any exit call.


                Something like this in checkNetHSM.nsh:


                #Error happens here
                echo BladeLogicReturnCode 1
                exit 1

                #normal exit code
                echo BladeLogicReturnCode 0
                exit 0


                And then the call can grep & cut the return code:


                runscript -h $1 -p 50 -e /usr/nsh/storage/scripts/checkNetHSM.nsh | grep BladeLogicReturnCode | cut -f2 -d' '


                • 5. Re: Feeding return code back from runscript

                  I've added the following code to the type1 script, immediately after the nexec line.



                  echo BladeLogicReturnCode $RC

                  exit $RC


                  I've then added the suggested grep and cut commands to the runscript command in the type2 script that calls the type1 script, but this just blocks all the stdout from the nexec, and prints the return code from each target server on stdout instead.


                  I don't want to affect the stdout from the nexec command in the type1 script.

                  • 6. Re: Feeding return code back from runscript

                    If you need to keep the output as well, you can redirect to a file, grep the return code and cat the file for the original output.


                    #run the script, send output to /tmp/file (could use tee here)
                    runscript -h $1 -p 50 -e /usr/nsh/storage/scripts/checkNetHSM.nsh > /tmp/file

                    #get the return code
                    VALUE=`grep BladeLogicReturnCode < /tmp/file | cut -f2 -d' '`
                    echo Return code is $VALUE

                    #Print the results as normal, remove grep -v if you want BladeLogicReturnCode
                    cat /tmp/file | grep -v BladeLogicReturnCode

                    rm /tmp/file


                    • 7. Re: Feeding return code back from runscript

                      This looks like it would work, but Richard has gone home now and I have no access to CM.


                      I will try it tomorrow and let you know.

                      • 8. Re: Feeding return code back from runscript

                        In case this doesn't work; can anybody think of any alternatives to using runscript that don't involve having two jobs?

                        • 9. Re: Feeding return code back from runscript

                          I'm not convinced.


                          Surely this is just going to display the return code for each target in the stdout at the job level in the 'show log' window.

                          It's not going to update the green tick/red cross that indicates a zero/non-zero return code.


                          Or am I missing something?

                          • 10. Re: Feeding return code back from runscript

                            OK - we've found a solution.


                            I noticed that the type1 script always had a line in stdout saying 'Exit Code ??'

                            The type2 script using runscript didn't have this.


                            My theory was that the GUI uses this line to work out the individual return codes and displays the correct Green Tick or Red Cross against the target in the Show Log screen.


                            To put the theory to the test, I modified my korn shell script on each of the target servers to output a line to stdout as 'Exit Code $RC'.


                            I then ran the type2 script detailed below and it worked.

                            It still works as a type1, but you just get two lines saying 'Exit Code ??' instead of one. So it appears that the bug in runscript is that it doesn't append the 'Exit Code' text to the target server output.


                            Type2 Script:


                            runcmd -h $1 -p 50 -NH -e nexec -e /usr/local/bin/mylocalscript.ksh

                            blcli -r Operations NSHScriptJob clearJobTargetList "/Job Folder" "Run my local korn shell script" >/dev/null


                            Note that this requires the %h macro setting up in the Parameters tab of the NSH script and that mylocalscript.ksh outputs the exact words 'Exit Code 0' or whatever your exit code is.


                            The alternative to modifying the korn shell script is to use runscript to call a type1 NSH script that echos the same words, but I think this is a little neater.

                            • 11. Re: Feeding return code back from runscript

                              You're absolutely right. The code looks for this particular "Exit Code" output. Take a look at this knowledge base article: