LSBInitScript – autossh-proxy

#!/bin/sh

### BEGIN INIT INFO
# Provides:          autossh-proxy
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop autossh-proxy
# Description:       autossh-proxy is a manager of socket5 proxy by ssh
### END INIT INFO

# Author: lizs <lizsmail.com>

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

NAME=autossh
AUTOSSH=/usr/bin/autossh
AUTOSSH_PIDFILE=/var/run/$NAME.pid
# AUTOSSH_LOGFILE=/var/log/$BASE.log
# autossh can use environment variables to control features, e.g. AUTOSSH_PID.
# more info see `man autossh`
AUTOSSH_PID="/usr/bin/env AUTOSSH_PIDFILE=${AUTOSSH_PIDFILE}"
# The autossh args
AUTOSSH_ARGS="-M 0 -fqTN -D 1080 root@alizs.cc"

# Get lsb functions
. /lib/lsb/init-functions

do_start()
{
    start-stop-daemon --start --quiet --oknodo \
        --pidfile $AUTOSSH_PIDFILE \
        --make-pidfile \
        --exec $AUTOSSH_PID $AUTOSSH \
        -- $AUTOSSH_ARGS
    RETVAL="$?"
    return "${RETVAL}"
}

do_stop()
{
    # Return
    # 0 if the daemon has been stopped
    # 2 if the daemon could not be stopped
    # other if a failure occured
    start-stop-daemon --stop --quiet --oknodo \
        --retry=TERM/5/KILL/5 \
        --pidfile $AUTOSSH_PIDFILE \
        --remove-pidfile
    RETVAL="$?"
    return "${RETVAL}"
}

do_restart()
{
    exit 0
}


case "$1" in
    status)
        if init_is_upstart; then
            exit 1
        fi
        status_of_proc -p "${AUTOSSH_PIDFILE}" "${AUTOSSH}" "${NAME}"
        ;;
    start)
        if init_is_upstart; then
            exit 1
        fi
        log_begin_msg "Starting ${NAME}..."
        do_start
        log_end_msg "$?" # e.g. [ok], failed!, (warning)
        ;;
    stop)
        if init_is_upstart; then
            exit 1
        fi
        log_begin_msg "Stopping ${NAME}..."
        do_stop
        log_end_msg "$?"
        ;;
    restart)
        if init_is_upstart; then
            exit 1
        fi
        log_begin_msg "Restarting ${NAME}"
        do_restart
        log_end_msg "$?"
        ;;
    force-reload)
        exit 0
        ;;
    *)
        log_failure_msg "Usage: service ${NAME} {status|start|stop|restart}"
        ;;
esac