Last active
April 14, 2024 09:59
-
-
Save admk/42f31728dc2a3ebb652a602cb8e7a4f0 to your computer and use it in GitHub Desktop.
autossh service (/etc/init.d/autossh)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# source: https://gist.github.com/suma/8134207 | |
# source: http://stackoverflow.com/questions/34094792/autossh-pid-is-not-equal-to-the-one-in-pidfile-when-using-start-stop-daemon | |
### BEGIN INIT INFO | |
# Provides: autossh | |
# Required-Start: $remote_fs $syslog | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: autossh initscript | |
# Description: establish a tunnelled connexion for remote access | |
### END INIT INFO | |
. /etc/environment | |
. /lib/init/vars.sh | |
. /lib/lsb/init-functions | |
TUNNEL_HOST=<HOST> | |
TUNNEL_PORT=<PORT> | |
TUNNEL_USER=<USER> | |
TUNNEL_FORWARD_PORT=<PORT> | |
SERVER_HOST=localhost | |
SERVER_PORT=22 | |
KEY_PATH=<KEY_PATH> | |
MONITOR_PORT=0 | |
NAME=autossh | |
DAEMON=/usr/bin/autossh | |
AUTOSSH_ARGS="-M $MONITOR_PORT" | |
SSH_ARGS="-F none -nNTv \ | |
-o ServerAliveInterval=30 -o ServerAliveCountMax=3 \ | |
-o IdentitiesOnly=yes -o ExitOnForwardFailure=yes \ | |
-i $KEY_PATH -R $TUNNEL_FORWARD_PORT:$SERVER_HOST:$SERVER_PORT \ | |
$TUNNEL_USER@$TUNNEL_HOST -p $TUNNEL_PORT" | |
DESC="autossh for reverse ssh" | |
SCRIPTNAME=/etc/init.d/$NAME | |
DAEMON_ARGS=" $AUTOSSH_ARGS $SSH_ARGS" | |
# Export PID for autossh | |
AUTOSSH_PIDFILE=/var/run/$NAME.pid | |
export AUTOSSH_PIDFILE | |
do_start() { | |
echo "Running: $DAEMON $DAEMON_ARGS" | |
start-stop-daemon --start --background --name $NAME --exec $DAEMON --test > /dev/null || return 1 | |
start-stop-daemon --start --background --name $NAME --exec $DAEMON -- $DAEMON_ARGS || return 2 | |
} | |
do_stop() { | |
start-stop-daemon --stop --name $NAME --retry=TERM/5/KILL/9 --pidfile $AUTOSSH_PIDFILE --remove-pidfile | |
RETVAL="$?" | |
[ "$RETVAL" = 2 ] && return 2 | |
start-stop-daemon --stop --oknodo --retry=0/5/KILL/9 --exec $DAEMON | |
[ "$?" = 2 ] && return 2 | |
return "$RETVAL" | |
} | |
case "$1" in | |
start) | |
log_daemon_msg "Starting $DESC" "$NAME" | |
do_start | |
case "$?" in | |
0|1) log_end_msg 0 ;; | |
2) log_end_msg 1 ;; | |
esac | |
;; | |
stop) | |
log_daemon_msg "Stopping $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) log_end_msg 0 ;; | |
2) log_end_msg 1 ;; | |
esac | |
;; | |
status) | |
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2 | |
exit 3 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment