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


  set -e

  # ........

  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?


1 Answer 1


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.

  • 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, 2020 at 19:45
  • @karneliy Maybe something is odd with your shell, try set +e and see how it goes Sep 30, 2020 at 19:54

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.