I'm trying to run shell script on remote server with my local shell script.
And I have to stop my local shell script when error occurred in remote shell script.

I'm running the remote shell script via ssh like this.

##parent shell script##
#!/bin/bash -ex
scp remote_shell.sh ${host}:${path}
ssh ${host} '${path}/remote_shell.sh ; rm ${path}/remote_shell.sh'
..do something
#!/bin/bash -ex
..do something
..error occurred
exit 1

The problem is, when remote_shell.sh exit with 1, but parent shell wouldn't stop.

I tried this script on local(without ssh), and parent stops as I expected.
Is this problem related to the third line ';' operator?
(I have to remove remote_shell.sh file regardless of failure.)

Of course I can achieve this with some If Else code, but I want to make it simple..



ssh ${host} '${path}/remote_shell.sh'


ssh ${host} '${path}/remote_shell.sh ; R=$?; rm ${path}/remote_shell.sh; exit $R'

Or some other way of preserving the exit code

  • Thanks! I works for my case! – tonarimochi Jun 6 '19 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.