Skip to content

Instantly share code, notes, and snippets.

@callado4
Forked from atr000/autossh init.d
Last active May 14, 2020 05:41
Show Gist options
  • Save callado4/5779213 to your computer and use it in GitHub Desktop.
Save callado4/5779213 to your computer and use it in GitHub Desktop.

Revisions

  1. callado4 revised this gist Jun 14, 2013. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion autossh init.d
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    #! /bin/sh
    #
    # Author: Andreas Olsson <[email protected]>
    # autossh init.d This script starts and stops an autossh daemon
    #
    # chkconfig: 2345 95 15
    # processname: autossh
    #
    # Original Author: Andreas Olsson <[email protected]>
    # Version: @(#)autossh_tunnel.foo 0.1 27-Aug-2008 [email protected]
    #
    # For each tunnel; make a uniquely named copy of this template.
  2. @atr000 atr000 created this gist Oct 24, 2010.
    84 changes: 84 additions & 0 deletions autossh init.d
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #! /bin/sh
    #
    # Author: Andreas Olsson <[email protected]>
    # Version: @(#)autossh_tunnel.foo 0.1 27-Aug-2008 [email protected]
    #
    # For each tunnel; make a uniquely named copy of this template.


    ## SETTINGS
    #
    # autossh monitoring port (unique)
    MPORT=54321
    # the ssh tunnel to setup
    TUNNEL="-L 12345:localhost:12345"
    # remote user
    RUSER="foobar"
    # remote server
    RSERVER="host.domain.tld"
    # You must use the real autossh binary, not a wrapper.
    DAEMON=/usr/lib/autossh/autossh
    #
    ## END SETTINGS

    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

    NAME=`basename $0`
    PIDFILE=/var/run/${NAME}.pid
    SCRIPTNAME=/etc/init.d/${NAME}
    DESC="the tunnel"

    test -x $DAEMON || exit 0

    export AUTOSSH_PORT=${MPORT}
    export AUTOSSH_PIDFILE=${PIDFILE}
    ASOPT=${TUNNEL}" -f -N "${RUSER}"@"${RSERVER}

    # Function that starts the daemon/service.
    d_start() {
    start-stop-daemon --start --quiet --pidfile $PIDFILE \
    --exec $DAEMON -- $ASOPT
    if [ $? -gt 0 ]; then
    echo -n " not started (or already running)"
    else
    sleep 1
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
    --test --exec $DAEMON > /dev/null || echo -n " not started"
    fi

    }

    # Function that stops the daemon/service.
    d_stop() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
    --exec $DAEMON \
    || echo -n " not running"
    }


    case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
    ;;

    restart)
    echo -n "Restarting $DESC: $NAME"
    d_stop
    sleep 1
    d_start
    echo "."
    ;;
    *)
    echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
    exit 3
    ;;
    esac

    exit 0