1


I need help with some scripts I'm writing.
Scenario:

  • Script A is executed by a scheduling process. This script takes the arguments passed to it, parses them in some way and runs script B feeding it with those arguments;
  • Script B does sudo -u user ssh user@REMOTEMACHINE, runs some commands (in the remote machine) and finally runs script C (also in the remote machine). I am passing those commands using a HERE DOCUMENT. Also, I'm passing the previous arguments to this script too.
  • This "flow" runs correctly and the job completes successfully.

My problems are:

  • Since this "flow" is ran by a scheduling process, I need to tell it if the job completed successfully or not. I'm doing this via exit codes, so what I want is to have a chain of exit codes, returning back from the last script to the first, in case of errors. I'm not able to perform this, because exit codes works correctly for the single scripts (I tried executing them singularly and look for the exit codes), but they are not sended back to the parent script. In my opinion, the problem is that ssh is getting the exit code from the child script, which in fact ended successfully, because there was no error executing it: it's the command inside of it that gone wrong.
  • While the process works correctly, I still get this line:

    ssh: Could not resolve hostname : Name or service not known

But actually the script completes successfully.

I hope you understand what I wrote, I can eventually post my scripts here.

Thanks

O.


EDIT:
This are the scripts. There could be some problem with variable names because I renamed it quikly to upload the files.
Since I can't upload 3 files because of my low reputation, I merged them in a single file
SCRIPT FILE

6
  • 2
    Please give your scripts. Commented Nov 10, 2014 at 14:22
  • You just need to make each script return an error if one occured, instead of returning always 0 as "there was no error executing it" (even though a command inside went wrong!). then you could maybe chain them with "&&" instead of ";" if each script has to make sure the previous one(s) ended successfully Commented Nov 10, 2014 at 15:56
  • @OlivierDulac I don't understand what you said. I tried to chain the commands in the second script with the && command, and also tried executing them separately (one ssh for kinit and then another ssh to launch the script) but the problem remains. Commented Nov 10, 2014 at 16:21
  • you need to change some things in your scripts. For example, in the ssh ... << ... SSHREMOTECOMMANDSFOREXPORT part, replace the if [ $? -gr 0 ] with if [ \$? -gr 0 ], so that the $? arrives as $? on the remote host via ssh, instead of being replace by 0 locally by your calling shell (as the last local command was probably ok). Same thing in other places, wherever you want $ on the remote machine, use \$ locally in most cases (to explain all the cases is complex, but in here sttrings, and inside simple quotes, use \$ to avoid $ being interpreted locally) Commented Nov 10, 2014 at 16:27
  • You can just put each script in your question : insert 2 newlines, and indent each of the script lines with 4 spaces. Separate with 2 newlines each scripts. Commented Nov 10, 2014 at 16:30

1 Answer 1

1

I managed to solve the problem.
I followed olivier's advice and used the escape char to make the variable expanded by the remote machine.
Also I implemented different exit codes based on where the error occured.
At last, I modified the first script as follows, after launching sudo -u for the second script:

EXITCODEOFTHESECONDSCRIPT=$?    
if [ $EXITCODEOFTHESECONDSCRIPT = 0 ]
    then
        echo ""
        echo "Export job took $SECONDS seconds."
        echo ""
        exit 0
else
    exit $EXITCODEOFTHESECONDSCRIPT
fi  

This way I am able to exit the main script MAINTAINING the exit code provided from the second script. In fact, I found that the problem was that the process worked well, even in case of errors, but the fact that I was giving more commands after the second script fail (the echo command was enough) provided other exit codes that overwrited the one I wanted.

Thanks to all !

Sign up to request clarification or add additional context in comments.

1 Comment

I knew you'd make it :) A usual approach is to record the exit code of important steps with something like errcode=$? just after that step, and then you can return $errcode to return from a function or exit $errcode to exit from the script with that same code, whatever echo/printf/etc you did after the important step

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.