2

I am trying to get the status code out of an Rscript run in an non-interactive way in the form of a bash script. This step is part of larger data processing cycle that involves db2 scripts among other things.

So I have the following contents in a script sample.sh:

Rscript --verbose --no-restore --no-save /home/R/scripts/sample.r >> sample.rout

when this sample.sh is run it always returns a status code of 0, irrespective of if the sample.r script run fully or error out in an intermediate step.

I tried the following things but no luck

1 - in the sample.sh file, I added an if and else condition for a return code like the below, but it again wrote back 0 despite sample.r failing in one of the functions inside.

if Rscript --verbose --no-restore --no-save /home/R/scripts/sample.r >> sample.rout
then
  echo -e "0"
else
  echo -e "1"
fi

2 - I also tried a wrapper script, like in a sample.wrapper.sh file

r=0
a=$(./sample.sh)
r=$?

echo -e "\n return code of the script is: $a\n"
echo -e  "\n The process completed with status: $r"

here also I did not get the expected '1' in the case of failure of the sample.r in an intermediate step on both the variables a and r. Ideally, i would like a way to capture the error (as '1') in a.

Could someone please advice how to get rscript to write '0' only in case of completion of the entire script without any errors and '1' in all other cases?

greatly appreciate the input! thank you!

Stewart Macdonald
  • 2,062
  • 24
  • 27
S2850951
  • 182
  • 8
  • Does this help: https://stackoverflow.com/questions/7681199/make-r-exit-with-non-zero-status-code – Stewart Macdonald Jun 13 '19 at 23:03
  • What is your Rscript like? If I run Rscript on a file with unknown functions/improper syntax I see the expected exit code of 1 – Rorschach Jun 14 '19 at 05:52
  • @jenesaisquoi my Rscript is a process that involves fetching data from a db2 database, doing computation, and writing data back to the db2 database among other things. my issue is, if the process fails in any of the steps, R writes an error back to the console and it ends right there. but the return code it writes is still '0'. but i would expect a return code of '1' in case of any error in the process. – S2850951 Jun 14 '19 at 13:26
  • @StewartMacdonald i have checked this post out earlier, i am not sure how to get it to work for my use case. – S2850951 Jun 14 '19 at 13:27

3 Answers3

2

I solved the problem by returning the status code in addition to echo. below is the code snipped from sample.sh script. In addition, in sample.R code i have added trycatch to catch the errors and quit(status = 1).

function fun {

if Rscript --verbose --no-restore --no-save /home/R/scripts/sample.r > sample.rout 2>&1
then
  echo -e "0"
  return 0  
else
  echo -e "1"
  return 1
fi

}

fun

thanks everyone for your inputs.

S2850951
  • 182
  • 8
0

The above code works for me. I modified it so that I could reuse the function and have it exit when there's an error

Rscript_with_status () {

rscript=$1

if Rscript --vanilla $rscript
then
        return 0
else
        exit 1
fi

}

run r scripts by:

Rscript_with_status /path/to/script/sample.r
SplitInf
  • 76
  • 4
-1

Your remote script needs to provide a proper exit status. You can make a 1st test by providing i.e. "exit 1" at the end of the remote script and see that it will make a difference.

remote.sh:

#!/bin/sh
exit 1

From local machine:

ssh -l username remoteip /home/username/remote.sh
echo $?
1

But the remote script should also provide to you the exit status of the last executed command. Experiment further by modifying your remote script:

#!/bin/sh
#exit 1
/bin/false

The exit status of the remote command will now also be 1.

ennox
  • 206
  • 1
  • 5