0

I have a bunch of bash commands that are run via ssh:

#!/bin/bash

CMD="
  set -e

  cmd1
  cmd2
  # ........


  service my_service stop

  # .........
  # some commands here......
  # .........

  service my_service start

" 

ssh -t -A -q -C $SERVER@$HOST "$CMD"

Sometimes, whenever my_service happens to be in the stopped status already, the command service my_service stop will return this:

--rpc-eval : RPC failed with reason :nodedown       # it's an Erlang app, hence a response
./my_script.sh: línea 26: error: orden no encontrada

Since the said command is deemed failure, for some reason, it'll terminate the execution, exit a session and thus won't reach the command service my_service start.

But I want it to always run both stop and start and the commands in between, of course, regardless of the state of an app before the command service stop, and not to fail, or move on despite a possible failure after "service stop"

How to do it?

karneliy
  • 21
  • 1
  • 5

1 Answers1

0

Your script specifies set -e, which causes the shell to exit if any command in the script returns an error. If you omit this, then by default the script will continue if any command returns an error.


If you wish to keep set -e and only continue if specific commands return an error, you can make the script ignore the errors from a command by using the control operator ||, which tells the shell to execute another command if the first command fails. And then by specifying the builtin command :, which does nothing but returns successfully (similarly to the true command, which you could also use).

So for example:

service my_service stop || :

If the service command fails, then the shell will execute : which will instead return success, and the script will continue.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • actually, neither commenting out "set -e", nor using the collon have helped, it'll still terminated an ssh session if an app has already been in the stopped state. The rc.d script itself works – karneliy Sep 30 '20 at 19:45
  • @karneliy Maybe something is odd with your shell, try `set +e` and see how it goes – Michael Hampton Sep 30 '20 at 19:54