Autossh is a nice way to keep a ssh connection alive. This is especially useful when it comes to ssh tunnels. To make things even more automagicial I have now written the init script template autossh_tunnel.foo.

A few import facts regarding the script:

  • Autossh 1.4 or later is required. Earlier versions of autossh doesn’t handle PID-files.
  • The init script is based on the start-stop-daemon. Hence it will probably only work on Debian, Ubuntu and similar systems.
  • There is no way to enter a password. A setup based on ssh-keys or similar is required.
  • Autossh doesn’t handle every kind of ssh problem. Because of that it is possible for the initial connection to fail without the init script knowing about it.

This is by the way my first real init script. Any feedback on it would be greatly appreciated.

  1. Exactly what I needed, and works flawlessly, thanks!
    Added the tunnel script in /etc/init.d/, “chmod +x tunnel”, and “update-rc.d tunnel defaults” so it starts on reboot.

  2. I am running autossh on behalf of a user called deploy. This required the following changes to the script

    d_start() {
    touch $PIDFILE
    chown deploy $PIDFILE
    start-stop-daemon -c deploy –start –quiet –pidfile $PIDFILE

    • Good point. In case I get around to rewriting this init script to a more modern init system I’ll make sure to include the option to specify what user to run as. Thanks.

Leave a Comment