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

2
  • 1
    do you have this problem? Why doesn't my Bash script recognize aliases? Jun 21, 2022 at 3:17
  • I tried shopt -s expand_aliases; ssh myserver 'true'; echo $? and now it gives 0! Thanks @αғsнιη! Maybe you could add as another answer? I guess both answers are correct for different reasons.
    – lonix
    Jun 21, 2022 at 3:46

1 Answer 1

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
2
  • 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, 2022 at 3: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). Jun 21, 2022 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .