0

I've aliased ssh to autossh -M0 for convenience, and it works well.

However it always exits in error, which causes problems with my scripts.

e.g.

ssh me@myserver 'true'      # equivalent to:  autossh -M0 me@myserver 'true'
echo $?     # 1

How do I fix this?

(A workaround in scripts, it to use /usr/bin/ssh instead of ssh. But I'd like to fix this if possible.)

lonix
  • 1,723

1 Answers1

1

From the man page:

There is a "starting gate" time. If the first ssh process fails within the first few seconds of being started, autossh assumes that it never made it "out of the starting gate", and exits. This is to handle initial failed authentication, connection, etc. This time is 30 seconds by default, and can be adjusted (see the AUTOSSH_GATETIME environment variable below).

So, setting AUTOSSH_GATETIME to zero solves that:

AUTOSSH_GATETIME=0 ssh me@myserver 'true'
echo $?     # 0

If you are not typing a password or passphrase, you can also use the -f flag of autossh (which you could add to the alias):

ssh -f me@myserver 'true'
echo $?     # 0
  • That's weird, because if ssh failed then I'd understand why this works... but ssh doesn't fail. Regardless, it works for me, so thank you Eduardo! – lonix Jun 21 '22 at 03:37
  • 1
    Well, the manpage is not clear, but the actual behaviour in the source code is this: if ssh exits before AUTOSSH_GATETIME then it is considered a premature exit, regardless of the actual ssh exit code. If you set AUTOSSH_DEBUG you will will see either ssh exited prematurely with status 0; autossh exiting (exit code 1) or ssh exited with status 0; autossh exiting (no mention to premature, exit code 0). – Eduardo Trápani Jun 21 '22 at 13:45