Skip to content

Instantly share code, notes, and snippets.

@jeremija
Created August 15, 2015 08:58
Show Gist options
  • Save jeremija/0d4533f3c93cc9e4c479 to your computer and use it in GitHub Desktop.
Save jeremija/0d4533f3c93cc9e4c479 to your computer and use it in GitHub Desktop.
raspi autossh
#! /bin/sh
# /etc/init.d/autossh
### BEGIN INIT INFO
# Provides: autossh
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts autossh at boot.
# Description: Starts autossh at boot.
### END INIT INFO
# If you want a command to always run, put it here
UID=pi
GID=pi
PID=/var/run/autossh.pid
MONITOR_PORT=2222
FW_PORT=3333
SSH_PORT=22
SSH_KEY=/home/$UID/.ssh/id_rsa
SERVER_USER=pius
SERVER_HOST=192.168.10.10
SERVER_PORT=22
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting autossh..."
if start-stop-daemon -b --start --chuid $UID --group $GID -m \
--pidfile $PID \
--startas /usr/bin/autossh \
-- \
-M $MONITOR_PORT \
-i $SSH_KEY \
-o "TcpKeepAlive yes" \
-o "ServerAliveInterval 240" \
-N -Rlocalhost:$FW_PORT:localhost:$SSH_PORT \
$SERVER_USER@$SERVER_HOST -p $SERVER_PORT
then
echo "Started autossh."
fi
;;
stop)
echo "Stopping autossh..."
if start-stop-daemon --stop --user $UID --pidfile $PID --name autossh
then
echo "Stopped autossh."
fi
;;
status)
if start-stop-daemon --status --user $UID --pidfile $PID --name autossh
then
echo "Process is running." 1,1 Top
else
echo "Process is stopped."
fi
;;
*)
echo "Usage: service autossh {start|stop|status}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment