Create a gist now

Instantly share code, notes, and snippets.

autossh init script for keeping up a connection to a VPS
### BEGIN INIT INFO
# Provides: sshvps
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Tunnel to VPS
# Description: This runs a script continuously in screen.
### END INIT INFO
case "$1" in
start)
echo "Starting sshvps"
su chris -c "screen -dmS sshvps /usr/lib/autossh/autossh -M 20001 -D 0.0.0.0:11111 me@myvps.mydomain.com"
;;
stop)
echo "Stopping sshvps"
PID=`ps -ef | grep autossh | grep 20001 | grep -v grep | awk '{print $2}'`
kill -9 $PID
;;
restart|force-reload)
echo "Restarting sshvps"
PID=`ps -ef | grep autossh | grep 20001 | grep -v grep | awk '{print $2}'`
kill -9 $PID
sleep 15
su chris -c "screen -dmS /usr/lib/autossh/autossh -M 20001 -D 0.0.0.0:11111 me@myvps.mydomain.com"
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment