autossh always exits in error

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.


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.)

Asked By: lonix


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
Answered By: Eduardo Trápani
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.