I have set up the following upstart script:

# autossh

description     "autossh connections"

start on started dbus
stop on (runlevel [06] or stopped dbus)

respawn limit 5 60 # respawn max 5 times in 60 seconds

    export AUTOSSH_PIDFILE=/var/run/autossh.pid
    export AUTOSSH_POLL=60
    export AUTOSSH_FIRST_POLL=30
    export AUTOSSH_DEBUG=1
    exec sudo -H -u pi -s autossh -M 0 \
         -R remoteport: [email protected]
    exec sudo -H -u pi -s autossh -M 0 \
         -R remoteport2: [email protected]
end script

but every time after the start I get this error message:

ssh exited prematurely with status 0

How can I fix it?


  • @Falcon Momot Or how about this remove the part about "raspbmc" and it's completely relevant and just helped me out immensely doing real live server administration. This should NOT be closed or downvoted. Commented Dec 28, 2015 at 18:27

2 Answers 2


You need a -N option so that is doesn't create an interactive shell.

exec sudo -H -u pi -s autossh -M 0 -N \
     -R remoteport: [email protected]

From the manual pages (man):

-N Do not execute a remote command. This is useful for just forwarding ports


Usually status 0 means that it was successful, but in your case I think that it means that it was successful in connecting but closed when it couldn't bind to the specified ports.

If remoteport[2] is a privileged port 1-1024, you wouldn't be able to bind to it unless you are root. You could change it and still leave localport to be the same.

If there is something already listening on the port you wouldn't be able to bind to it either.

The first thing you should do if my answers above were not 100% accurate, is to connect manually with ssh (removing the -M, adding -v):

ssh -v -Rremoteport: [email protected]

Also, you should combine both -R statements into 1 ssh command, you don't want to run it as two if they are both connecting to the same server, like this:

ssh -v -Rremoteport: -Rremoteport2: [email protected]

See what errors you get running the regular ssh command, but it's probably the port you chose.

  • sudo -H -u pi -s autossh -M 0 -R remoteport: -p sshport -i /home/pi/.ssh/id_dsa [email protected] works but the upstart script exits immediately with the above error message. Do I have to add some switches like -f ?
    – Peleke
    Commented Jul 2, 2013 at 13:16

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